The following code fails to create a valid icon recognized by Windows Explorer or VS2005; though, it can be opened in Paint.
using (Bitmap bmp = new Bitmap(@"C:\ABitmap.bmp"))
{
bmp.Save(@"C:\IconFromBitmap.ico", ImageFormat.Icon);
}
After googling, a post in MSDN forum was found with similar problem
http://forums.microsoft.com/msdn/showpost.aspx?postid=63447&siteid=1
The code seems rather straight-forward but it does not appear to do the job.
Does anyone have a solution?
-
-
It's a known bug. The .NET GDI doesn't even have an icon encoder so it saves it as a png file. I was trying to find a solution for that for a long time and the only thing I've found is that you can use a free image conversion library called FreeImage ( http://freeimage.sourceforge.net/ ) which can save images to many different icons. It's free and released under the GNU license if I'm not mistaken, but there's one limit - it can save icons only in this size range: 16x16 - 128x128 . So keep in mind that smaller or bigger images won't be saved.
If you find a better solution please share it with us. I'd really like to know how to solve it without using an external source. -
Since *.ico files are simply binary files containing an embedded resource list containing bitmap icons.
So couldn't you create an *.ico file using .NET's resource manipulation namespaces?
-
What are the .NET's resource manipulation namespaces?
-
yyy wrote:What are the .NET's resource manipulation namespaces?
System.Resources
-
Thanks but I don't think I know how to use it in order to create the icon. An icon file's layout is quite complicated, as explained in this article: http://www.codeproject.com/dotnet/MultiIcon.asp . I think that if you need to convert a bitmap to an icon you'll first have to get a lot of information from that bitmap and that's the hard part. But maybe there's something I don't know about.
-
If you are willing to put one per file, you can just save it as a bitmap with the ICO extension.
Just make sure it is 16x16, 32x32, or 64x64 if you want to work in most places.
Jorgie
-
Jorgie, by just changing the file extension, Windows Explorer seems to recognize it. But when it is used as a Windows Form icon, VS2005 throws an error, "Argument 'picture' must be a picture that can be used as a Icon".
After some reading, it appears that to correctly do it, I have to manupulate Bitmap headers and Icon headers / structs myself based on Windows API documentation; and there will be quite some coding. That is rather odd to me since it is a known problem; nonetheless .NET 2.0 chooses not to address it.
Thanks guys for the replies. I will attemp to do some coding and it is surely going to be an interesting experience.
-
Great - don't forget to tell us if you find out how to do it right. I'm also interested in this. BTW, maybe this can help you: http://www.codeproject.com/dotnet/safeicon.asp - it shows a code that can convert an Image object to an icon but it doesn't work with all sorts of icons.
-
has this been fixed in .NET 3.5 (maybe WPF)?yyy said:Great - don't forget to tell us if you find out how to do it right. I'm also interested in this. BTW, maybe this can help you: http://www.codeproject.com/dotnet/safeicon.asp - it shows a code that can convert an Image object to an icon but it doesn't work with all sorts of icons.
i tried the freeimage lib, the sample for .NET didn't even compile. -
the .NET framework's class libraries have only been extended in .NET 3.0 and 3.5, all the core assemblies remain the same. So no.googlexp said:
has this been fixed in .NET 3.5 (maybe WPF)?yyy said:*snip*
i tried the freeimage lib, the sample for .NET didn't even compile.
Re: Jorgie: that wouldn't work because an ICO is application/x-icon, Explorer is just more tolerant and loads up the Win32 bitmap reader despite the file extension, a proper icon reader would choke on it.
-
System.IO.Stream outputStream;
Icon tIco;
Bitmap tBmp = new Bitmap(pictureBox1.Image);
outputStream = System.IO.File.Create(@textBox2.Text);
tIco = Icon.FromHandle(tBmp.GetHicon());
tIco.Save(outputStream);
outputStream.Close();
----
The only problem I have with the code it the apparent loss of colors. Though the end result does show up in properties as 32bit.
This will allow you to use the icon as an icon on your forms.
-Chazm
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.