Posted By: Mar7in | Nov 5th, 2005 @ 8:08 PM
page 1 of 1
Comments: 11 | Views: 25927
Mar7in
Mar7in
"The Dream of Infinite Processing Power or the Nightmare of Reality"
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?
yyy
yyy
Developer
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.
W3bbo
W3bbo
The Master of Baiters
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?

yyy
yyy
Developer
What are the .NET's resource manipulation namespaces?
W3bbo
W3bbo
The Master of Baiters
yyy wrote:
What are the .NET's resource manipulation namespaces?


System.Resources
yyy
yyy
Developer

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.

Jorgie
Jorgie
Jorgie

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

yyy
yyy
Developer
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)?

i tried the freeimage lib, the sample for .NET didn't even compile.
W3bbo
W3bbo
The Master of Baiters
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.

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
page 1 of 1
Comments: 11 | Views: 25927
Microsoft Communities