Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert image to icon in c#

Tags:

I have a project that converts an image format file into an icon file. However, after converting the image, the color of the image changes.

Here is my code

Bitmap theBitmap = new Bitmap(theImage, new Size(width, height)); IntPtr Hicon = theBitmap.GetHicon();// Get an Hicon for myBitmap. Icon newIcon = Icon.FromHandle(Hicon);// Create a new icon from the handle. FileStream fs = new FileStream(@"c:\Icon\" + filename + ".ico", FileMode.OpenOrCreate);//Write Icon to File Stream 

enter image description here

Anybody know how to solve this?

like image 953
r.vengadesh Avatar asked Jun 20 '13 11:06

r.vengadesh


People also ask

How do I edit an .ICO file?

In Solution Explorer, double-click the file app. ico. The Image Editor will open. Go to menu Image > Tools and select Text Tool.

How do I convert PNG to ICO in paint?

Select your PNG, then click Open. Click Upload. Crop your image as needed, then scroll down and click Select None. Scroll all the way down and click Convert ICO.


1 Answers

Bitmap.GetHicon() is very good at creating icons that work well on any Windows version that can run .NET code. Including the old ones, Windows 98 and Windows 2000. Operating systems that did not yet support fancy icons.

So what you get is an icon with only 16 colors, using a pre-cooked palette with basic colors. This tends to generate disappointing results, to put it mildly.

The Bitmap or Icon classes do not have an option to get a better result. In general you'll need to use an icon editor to create good icons. Which should include multiple images in different sizes and color depths so they'll work well with any video adapter setting and any operating system version. Particularly color reduction from 16 million to 256 or 16 colors is a non-trivial operation with multiple ways to do it, none of them perfect. A good icon editor has the tools you need to make that work well enough.


UPDATE: getting to be a very dated problem, XP is yesteryear. Today you can generate a very good looking icon with this code.

like image 89
Hans Passant Avatar answered Sep 20 '22 22:09

Hans Passant