Get WPF single path data from . png file - wpf

I would like to convert png file(transparent icon) to WPF "path data".
Any idea how to get single wpf path data from .png icon.?

There is no direct way to do it. A PNG file is a raster format (i.e. it stores the colour of each pixel in the image). WPF Path Data is a vector format (i.e. it stores the image as geometric drawing instructions). If you are unsure what this means, see here for more info.
Vector can be converted to raster (at a set size), but raster cannot be obviously converted to vector (which is what you want).
The only way to try and convert raster to vector, with varying results, is to "trace" the raster image to guess what the equivalent geometric vector instructions might possibly be. The ability to trace a raster image accurately is directly proportional to its pixel dimensions and complexity of graphics. So tracing a [presumably] small icon might not be possible at all.
If I were faced with your problem, I would get as high a quality PNG image as possible, import it into Adobe Illustrator, use the Illustrator tracing tools to trace the image, and finally export the result of the trace to XAML (using something like http://www.mikeswanson.com/xamlexport/).
If you are looking for runtime tracing, this is something I have not come across. Given the massively varying tracing parameters which are required for different styles of raster images, I don't suppose it would even be realistically possible.
Good luck.

Related

How can I determine the colorspace (RGB) profile of my data?

I have a standard jpeg image, which I use within some commercial software to colorize other data (by mapping the image's color onto the data). Then I export the colored data from this software to an XYRGB ascii file, i.e. I store the data information in the first two columns of each row and then the three RGB colors in the last three columns.
Since I need to convert the color to CIELab or CIELuv, it seems I need to know which exact colorspace (RGB, sRGB, gamma, whitepoint - you name it) my RGB values are in. But the question is: How can I find out? Or could I just assume a certain profile being a good approximation?
(Remark: The company of the commercial software I used was not able to tell me any specifics...)
If you don't know the provenance of the image, there's not anything you can do to determine the color space from the RGB data alone. It's a little like having a blueprint without a scale. You could guess and check with an application like Photoshop that can assign a profile to an image but even then it's not always obvious which is correct unless the image contains colors you can recognize as correct.
For many images sRGB is good guess. Most image on the web are sRGB and many non-color managed apps assume sRGB. But just understand that it is still a guess. If color accuracy is critical, you need the profile.

Which is better? Between using image file and draw vector shape

As I said on the title.
I just want to know which is better between using image files and drawing vector shapes (or path).
I know that using vector is better for appearance but what about performance.
And if this depends on cases. Can anyone explain.
(This question may include WP7, Silverlight, WPF or even in general cases.)
Here is a general answer to compare pros/cons of Bitmap (what I think you mean by "image file") vs. Vector.
Bitmap-based images (gif, tiff, jpeg, png, bmp) are essentially the concept of mapping colours (and other data such as alpha layer) to a pixel grid. Different file formats offer variations of what is supported and levels of compression but this is the high-level concept. The complete map of pixels and data is stored in the file as a matrix/table.
Vector-based images, as you say, are path based. Instead of storing information by pixels, the file format will store geometric points and data.
The pros for bitmaps are:
They usually render faster than a vector. This is because there is minimal computation involved in presenting the image (just take the pixel map and display).
They handle "photographic" content better than a vector.
They are more portable than vector. GIF, JPEG, PNG, BMP are more standard than any vector format (where usually Adobe has the market)
The cons for bitmaps are:
They don't scale without degradation (pixelization)
Manipulation (i.e. resizing, blurring, lighting, etc) of a bitmap is more processor expensive than a vector
The files are usually much larger than vector-based files
The pros for vectors are:
Flexible for scaling and manipulation
Smaller file formats than vector
Ideal for print and animation (i.e. manipulating a shape to produce the animation effect)
The cons for vectors are:
Render time, depending on the complexity of the vector, can be longer
Portability most formats are highly proprietary
Work for "graphic" based images but not useful for photorealism
Hope this helps.
Jeremiah Morrill gave a great overview of WPF rendering that basically shows a vector will always be more expensive to render than an image. Basically an image gets treated as a directx texture...no matter the size, scaling or whatever, there is a set constant cost for rendering an image. As Jer's overview shows, even the simplest vector image takes a number of operations to render in WPF. The moral of the story is that when giving an option, go for the image instead of vector.
Based on our experience with Windows Phone 7 (Non-mango) apps, we find using Images instead of using drawing produces a far more responsiveness hence UX Performance for continuous animation in pages. (YMMV)
I would initially say that images render faster than vectors. The complexer the vector, more time it takes to render. The bigger the image, more time to render.
I'm going to speculate that (in Silverlight terms) most of the current video hardware is capable of directly handling the images rendering getting so a boost in the performance. I'm not sure if calculations for vectors can be done at video hardware level.
From the point of view of Windows Phone 7, you'll typically get faster rendering of images/bitmaps rather than paths/vectors. As a general rule for mobile development, due to the constrained resources on the device and the increased need to consider performance, if you can do something once, such as preparing an image, at design (or compile) time that definitely preferable to doing it multiple times on each client.
Be very careful of applying rules across platforms (WPF, Silverlight & WP7) as they are used for different things in different situations and are under different constraints. Things you have to consider on the phone may not be as much of an issue in a WPF app running on an high powered PC.

How should I use .ico files in a Winforms Application?

I'm developing a WinForms c# 3.0 application. Our designer created quite a lot of .ico files containing all the needed art. The choice of .ico was made because quite often, the same image is needed in several places in different dimensions.
Now, it seems .ico files are really annoying to use in visual studio. The only way to use those images seems to be through images list (which aren't supported by all controls).
Compared to other resources, you can't write this :
foo.Image = global::RFQHUB.RFQHUBClient.Properties.Resources.foo; // Cannot implicitly convert type 'System.Drawing.Icon' to 'System.Drawing.Image'
Here are the options I'm considering :
create ImageLists of all possible sizes referencing all my icons in my main window. Link these ImageLists from other windows and find a way to export Image objects from the ImageList when I can't use it directly ; since ImageList contains a Draw() method, this should probably be possible.
convert all the x.ico I've got in several x16.gif ...x48.gif, and use those through resources.
I'd be interested to know if some people have been successfully using .ico resources in a Winform application. In so, how did you set up things ?
ICO isn't quite an obsolete format, but it's close. It's still useful for your application icon, but for almost everything else, it's better to use an ImageList for each size that you need. And it's much faster to populate an ImageList from a bitmap that contains multiple images layed out in a grid.
You also want to use an Alpha channel transparency in your bitmaps to get the best result, so storing them as .PNG files in your resources is the best way to go, since PNG supports an alpha channel. ICO and GIF files support only single bit for transparency - every pixel is either fully opaque or fully transparent. An 8 bit alpha channel for transparency looks much nicer.
If you can send your artist back to the drawing board then you should do so, and have him/her do full anti-aliased images with alpha. If you can't, then I suggest that you write a small program to convert all of your icon files into bitmaps suitable for loading into ImageLists.
Convert the images into PNG. Point. Whoever decided to use .ico files to start with should get talked to in private - the argument holds no ground.

Website Image Formats: Choosing the right format for the right task

When designing a website, what do you consider the best image format to use for a particular task?
I always find myself in a dilemma when trying to figure out what format to use for a specific task...like for example, should I use .jpg all round? or, when and why should I use a .png?
For example, taking Amazon's website, they use .jpg for product images (Example), .gif for this transparent pixel (Example) and .png for their CSS Sprites (Example)
On the other hand, Play.com use a .gif for their website logo (Example), but use .jpg for their website products (like Amazon) (Example) and as far as their main page goes, they dont have any .pngs on it.
So what formats should I use for my websites? and why should I use them?
[UPDATE]
Thanks CruellO for this link for explaining the differences, and also Dustin for giving reasons on what to use.
You should be aware of a few key factors...
First, there are two types of compression: Lossless and Lossy.
Lossless means that the image is made smaller, but at no detriment to the quality. Lossy means the image is made (even) smaller, but at a detriment to the quality. If you saved an image in a Lossy format over and over, the image quality would get progressively worse and worse.
There are also different colour depths (palettes): Indexed color and Direct color.
With Indexed it means that the image can only store a limited number of colours (usually 256) that are chosen by the image author, with Direct it means that you can store many thousands of colours that have not been chosen by the author.
BMP - Lossless / Indexed and Direct
This is an old format. It is Lossless (no image data is lost on save) but there's also little to no compression at all, meaning saving as BMP results in VERY large file sizes. It can have palettes of both Indexed and Direct, but that's a small consolation. The file sizes are so unnecessarily large that nobody ever really uses this format.
Good for: Nothing really. There isn't anything BMP excels at, or isn't done better by other formats.
GIF - Lossless / Indexed only
GIF uses lossless compression, meaning that you can save the image over and over and never lose any data. The file sizes are much smaller than BMP, because good compression is actually used, but it can only store an Indexed palette. This means that there can only be a maximum of 256 different colours in the file. That sounds like quite a small amount, and it is.
GIF images can also be animated and have transparency.
Good for: Logos, line drawings, and other simple images that need to be small. Only really used for websites.
JPEG - Lossy / Direct
JPEGs images were designed to make detailed photographic images as small as possible by removing information that the human eye won't notice. As a result it's a Lossy format, and saving the same file over and over will result in more data being lost over time. It has a palette of thousands of colours and so is great for photographs, but the lossy compression means it's bad for logos and line drawings: Not only will they look fuzzy, but such images will also have a larger file-size compared to GIFs!
Good for: Photographs. Also, gradients.
PNG-8 - Lossless / Indexed
PNG is a newer format, and PNG-8 (the indexed version of PNG) is really a good replacement for GIFs. Sadly, however, it has a few drawbacks: Firstly it cannot support animation like GIF can (well it can, but only Firefox seems to support it, unlike GIF animation which is supported by every browser). Secondly it has some support issues with older browsers like IE6. Thirdly, important software like Photoshop have very poor implementation of the format. (Damn you, Adobe!) PNG-8 can only store 256 colours, like GIFs.
Good for: The main thing that PNG-8 does better than GIFs is having support for Alpha Transparency.
Important Note: Photoshop does not support Alpha Transparency for PNG-8 files. (Damn you, Photoshop!) There are ways to convert Photoshop PNG-24 to PNG-8 files while retaining their transparency, though. One method is PNGQuant, another is to save your files with Fireworks.
PNG-24 - Lossless / Direct
PNG-24 is a great format that combines Lossless encoding with Direct color (thousands of colours, just like JPEG). It's very much like BMP in that regard, except that PNG actually compresses images, so it results in much smaller files. Unfortunately PNG-24 files will still be much bigger than JPEGs, GIFs and PNG-8s, so you still need to consider if you really want to use one.
Even though PNG-24s allow thousands of colours while having compression, they are not intended to replace JPEG images. A photograph saved as a PNG-24 will likely be at least 5 times larger than the equivalent JPEG image, with very little improvement in visible quality. (Of course, this may be a desirable outcome if you're not concerned about file size, and want to get the best quality image you can.)
Just like PNG-8, PNG-24 supports alpha-transparency, too.
JPEGs are for photos. I see JPEGs with text in them occasionally and they just look awful. Text is best for text, otherwise use PNG.
If it's not a photo, but you want a graphic of it, use a PNG. A PNG is almost always smaller than the equivalent gif and will not lose quality like a JPEG file. A PNG equivalent of a JPEG will typically be a lot larger (assuming it's photorealistic). There may be times where this is still desirable.
PNG does allow for 8-bits of transparency, but if you have to support IE, you'll find that they continually refuse to support that correctly. They do support a single bit of transparency in an 8-bit image (essentially the same as gif) as far as I know. There are also numerous hacks to get 8-bit transparency to work in IE. I've never bothered, myself.
In summary:
Photos → jpg
!Photos → png
PNG can be used when:
You need transparency (either 1-bit or alpha transparency)
Lossless compression will work well (such as a flat-style icon or logo)
JPEG can be used when:
Lossless compression will not work well (such as a photograph)
GIF can be used when:
Animation is necessary, and video is not possible (though you should really try and use video; animated GIFs are poor quality and very inefficient)
Despite myths to the contrary, PNG outperforms GIF in all like for like comparisons. PNG is capable of every image mode of GIF apart from animation, and when using the same image mode, PNG will have better compression due to its superior DEFLATE algorithm compared to LZW. PNG is also capable of additional modes that GIF cannot do, such as 24 bit color, and multi-bit transparency (alpha transparency). Note that multi-bit transparency used to be a problem back when people used IE6.
PNG modes include (this is just a small subset)
Palette colour of 2 to 256 colors (like GIF)
Palette colour of 2 to 256 colors, with transparent color (like GIF)
True color (24 bit color)
True color with alpha channel (24 bit color + 8 bit transparency)
For best compression in PNG for the web, always use a palette mode. If you find PNG files are larger than the equivalent GIF files, then chances are you're saving the PNG in 24 bit color and the GIF in palette mode (because saving a full color GIF always requires translation to palette mode). Try converting the image to palette mode before saving in both cases.
PNG also has other modes such as palette color with alpha transparency in the palette. Modes such as this work in browsers but software like Photoshop have (or once had) problems with creating or working with them due to not supporting those image modes.
If you are storing or presenting a large number of images the new Google WebP format might be worth considering as it is 25% smaller than PNG/JPG.
Note this is not supported by all browsers at the moment.
NB. This came out in 2010 after this question was posted.
JPEG FILE FORMAT
Great for images when you need to keep the size small
Good option for photographs
Bad for logos, line art, and wide areas of flat color
GIF FILE FORMAT
Great for animated effects
Nice option for clip art, flat graphics, and images that use minimal colors and precise lines
Good option for simple logos with blocks of colors
PNG FILE FORMAT
Lossless
Excellent choice when transparency is a must
Good option for logos and line art
Not supported everywhere
You can see this infographics for more detailed information, Image File Types: When to use JPEG, GIF & PNG

How to embed a png in an Adobe Illustrator or Expression Design file to create XAML

I have an AI file. I paste it into Expression Blend and then export the XAML for use in my WPF project. Works for most of my files, but some export the XAML plus a seperate png file. What can I do so that the png is embedded into the paths of my image and not a seperate image? Can it be done?
Checkout this page that describes converting raster graphics to vector and then XAML -
http://weblogs.asp.net/rrobbins/archive/2007/11/11/how-to-convert-raster-graphics-to-xaml.aspx
Another route that you could try is to use Expression Design to convert the .ai file.
(Please note that once you have Expression Design open, you need to create a new document before the File->Import menu item is even enabled.)
However, once you convert the .ai file with Expression Design, you will likely still have the problem of having some raster information in the .ai ... which as Terrapin already mentions is hard to convert to vector (and usually brings a high memory footprint to get even close to the quality of the raster image).
But to help you out there, Expression Design also comes with some ability to convert raster information into vector. If you select the image that you want to convert, just go to Object->Image->Auto Trace Image in order to convert it.
See this StackOverflow question for more info, but basically Microsoft allows you to download a trial that you can use for 90 days.
Hope that helps.
It sounds like the PNG that is generated is probably the raster part of your AI file, and it can't be converted to vector graphics. Are you importing PNGs, or JPGs, or another raster graphic into your AI file?
To convert raster images to vectorial images, I found Inkscape (free) to do an excellent job (comparable to VectorMagic, which is not free anymore by the way).
In Inkscape, import your image and use the Path/Trace Bitmap function. It has a lot of control.
Once you converted to a vectorial image, save it as a SVG. Then, using ViewerSvg, you can convert to XAML vector data. (Path & Canvas)

Resources