Convert png to high quality ico

This may be a very simple problem, but I have not yet found the perfect solution. I am trying to convert png to ico with C # and found the question of converting .PNG to .ICO in C / C # , which seems to give a working solution, as shown 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 slightly modified this approach:

 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 I am facing is that as a result ico is of poor quality, I assume that it has changed to 16x16 and lost some of the color, maybe now it has only 16 colors? How can I convert to a better ico file?

+6
source share
3 answers

I believe that you will need a more reliable method than GetHIcon() . It is rather a "quick and dirty" option, and by no means lost.

Here is an example of a class that can preserve image quality on the path to conversion as an ICO:

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

+7
source

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

+3
source

In this question, the decision made uses imagemagick, which is an excellent image manipulation tool that allows you to control the size, color depth, etc. when converting from png to ico. I would really suggest trying this solution.

Using imagemagick will look something like this:

convert -resize x16 -gravity center -crop 16x16 + 0 + 0 input.png \ -flatten -colors 256 output / favicon.ico

(You can control -resize and -colors to achieve what you are looking for.)

The same options should be available programmatically in C # through http://imagemagick.codeplex.com

+2
source

Source: https://habr.com/ru/post/948214/


All Articles