Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert png to high quality ico

Tags:

c#

icons

png

Maybe this is a very easy problem to solve but I haven't found the perfect solution yet. I'm trying to convert a png to ico with C# and found the question converting .PNG to .ICO in C/C# which sort of gives a working solution as below:

using (FileStream stream = File.OpenWrite(@"C:\temp\test.ico"))
{
    Bitmap bitmap = (Bitmap)Image.FromFile(@"c:\temp\test.png");
    Icon.FromHandle(bitmap.GetHicon()).Save(stream);
}

For my own project I have changed this approach slightly to:

string pngFile = "path/to/pngfile";
using (Bitmap bitmap = new Bitmap(pngFile))
{
   using (Icon icon = Icon.FromHandle(bitmap.GetHicon()))
   {
      using (MemoryStream stream = new MemoryStream())
      {
         icon.Save(stream);
         // something interesting with icon here
      }
   }
}

The problem that I am experiencing is that the resulting ico is of poor quality, I'm guessing it got resized to 16x16 and lost some of it's color depth, perhaps now only has 16 colors? How can I convert to a higher quality ico file?

like image 255
Bazzz Avatar asked Jun 27 '13 11:06

Bazzz


2 Answers

I believe you will need a more robust method than GetHIcon(). It is more of a "quick and dirty" option, and by no means loss-less.

Here's an example of a class that can preserve image quality on the way to converting as ICO:

https://gist.github.com/darkfall/1656050

like image 111
DonBoitnott Avatar answered Sep 30 '22 16:09

DonBoitnott


Check http://www.codeproject.com/Tips/627823/Fast-and-high-quality-Bitmap-to-icon-converter This is a clear and fast solution to convert a bitmap to png

like image 23
RamonEeza Avatar answered Sep 30 '22 17:09

RamonEeza