Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an Icon from a png image?

Tags:

icons

wpf

png

I'm creating an WPF app, so I'm mostly working with the ImageSource class for icons. However, the system tray icon has to be of type System.Drawing.Icon. Is it possible to create such an object from a png image?

I have tried the following:

private static System.Drawing.Icon _pngIcon;
public static System.Drawing.Icon PngIcon
{
    get
    {
        if (_pngIcon == null)
        {  
            //16x16 png image (24 bit or 32bit color)
            System.Drawing.Bitmap icon = global::BookyPresentation.Properties.Resources.star16;
            MemoryStream iconStream = new MemoryStream();
            icon.Save(iconStream, System.Drawing.Imaging.ImageFormat.Png);
            iconStream.Seek(0, SeekOrigin.Begin);
            _pngIcon = new System.Drawing.Icon(iconStream); //Throws exception
        }
        return _pngIcon;
    }
}

The Icon constructor throws an exception with the following message: "Argument 'picture' must be a picture that can be used as a Icon."

I figured it might be something with the bit depth of the image color as I had some issues with this earlier, but both 32bit and 24bit images didn't work. Is it possible what I'm trying to do?

like image 554
Morten Christiansen Avatar asked Dec 21 '08 20:12

Morten Christiansen


People also ask

Can PNG files be used as icons?

If you're loading the images from a resource file, then no, you can't use PNGs, you have to use ICOs. Fortunately, there are a number of tools that can convert PNGs into ICOs, including ImageMagick (great for automation), and MSPaint as a lowest common denominator.

Can I just rename a PNG to ICO?

Take any PNG file and rename it with a . ico extension. Really, that's all it is. See also The evolution of the ICO file format, part 4: PNG images.


3 Answers

I think you can try something like this before convert your image to .ico:

    var bitmap = new Bitmap("Untitled.png"); // or get it from resource     var iconHandle = bitmap.GetHicon();     var icon = System.Drawing.Icon.FromHandle(iconHandle); 

Where icon will contain the icon which you need.

like image 149
maxnk Avatar answered Oct 12 '22 12:10

maxnk


There's also a website (http://www.convertico.com/) which converts PNGs into ICOs.

like image 41
Stephen Wrighton Avatar answered Oct 12 '22 10:10

Stephen Wrighton


Icons are a combination of 3 or 4 image sizes:

48 × 48, 32 × 32, 24 × 24 (optional), and 16 × 16 pixels.

And can/should also contain three different colour depths:

  • 24-bit with 8-bit alpha (32-bit)
  • 8-bit (256 colors) with 1-bit transparency
  • 4-bit (16 colors) with 1-bit transparency

So the .png memory stream isn't going to fit into the icon's constructor. In fact, if you read the notes on the other constructor overloads, you'll see all the "Size" or Width and Height measurements for finding the correct size icon in the file.

More information on the manual creation of icons can be found under "Creating Windows XP Icons"

like image 34
Zhaph - Ben Duguid Avatar answered Oct 12 '22 12:10

Zhaph - Ben Duguid