The Bitmap class is NOT a paint program

I needed to convert a bitmap to an icon. After discovering Paint Shop Pro didn't support this (seemed odd to me), I thought I'd just quickly write up a three line .NET app to convert a bitmap to an icon.

My idea centered around the Bitmap class' Save method which accepts a filename and a format. "Perfect!", I thought. So I tapped in the code:

Bitmap bmp = new Bitmap(@"C:\folder\file.bmp");
bmp.Save(@"C:\folder\file.ico", ImageFormat.Icon);

Run it. Works! Try and use the ICO file in an application expecting a real Win32 ICON file? No go...

Can't take the shortcut on this one. You need to create an Icon from the Bitmap. Still very easy with the framework, but a few more lines of code is necessary:

Bitmap bmp = (Bitmap) Bitmap.FromFile(@"C:\folder\file.bmp");
Icon ico = Icon.FromHandle(bmp.GetHicon());
FileStream file = new FileStream(@"C:\folder\file.ico", FileMode.OpenOrCreate);
ico.Save(file);
file.Close();
ico.Dispose();

1 Comment

Comments have been disabled for this content.