I have a bitmap that I would like to display in a Windows form as an icon. I need to convert it to an icon before I can use it. The best solution that I have found is:
C-SHARP / C# Code:
string bitmapFileName = openFileDialog1.FileName;
// Retrieve the bitmap from the file.
Bitmap bmp = (Bitmap)Image.FromFile(bitmapFileName);
// Convert the bitmap to an icon.
Icon ico = Icon.FromHandle(bmp.GetHicon());
// Generate the icon file name
string iconFileName = bitmapFileName.Remove(bitmapFileName.Length - 4) + ".ico";
// Create a file stream to save the icon stream.
Stream st = new FileStream(iconFileName, FileMode.Create);
// Create a stream writer to physical write the data to the disk.
BinaryWriter wr = new BinaryWriter(st);
// Write the binary icon data to the file stream.
ico.Save(st);
// Close the file to write the stream to the disk.
wr.Close();
The problem with this is that I lose all of the color and get a gray icon. Does anyone know of a better way to do this (in code or a separate application like paint)?
Thanks,
fakepoo