Reputation: 648
I have problem with creating icon under windows xp. Here is my code:
void SaveAsIcon(Bitmap SourceBitmap, string FilePath)
{
FileStream FS = new FileStream(FilePath, FileMode.Create);
// ICO header
FS.WriteByte(0); FS.WriteByte(0);
FS.WriteByte(1); FS.WriteByte(0);
FS.WriteByte(1); FS.WriteByte(0);
// Image size
FS.WriteByte((byte)SourceBitmap.Width);
FS.WriteByte((byte)SourceBitmap.Height);
// Palette
FS.WriteByte(0);
// Reserved
FS.WriteByte(0);
// Number of color planes
FS.WriteByte(0); FS.WriteByte(0);
// Bits per pixel
FS.WriteByte(32); FS.WriteByte(0);
// Data size, will be written after the data
FS.WriteByte(0);
FS.WriteByte(0);
FS.WriteByte(0);
FS.WriteByte(0);
// Offset to image data, fixed at 22
FS.WriteByte(22);
FS.WriteByte(0);
FS.WriteByte(0);
FS.WriteByte(0);
// Writing actual data
SourceBitmap.Save(FS, ImageFormat.Png);
// Getting data length (file length minus header)
long Len = FS.Length - 22;
// Write it in the correct place
FS.Seek(14, SeekOrigin.Begin);
FS.WriteByte((byte)Len);
FS.WriteByte((byte)(Len >> 8));
FS.Close();
}
It works ok under vista and higher.
I don't have idea what i should change in my method to create icon from bitmap under windows xp.
When i try use icon with method above i get this error:
Error generating Win32 resource: Error reading icon
under vista, 7 and 8 it works.
Upvotes: 0
Views: 329
Reputation: 942308
SourceBitmap.Save(FS, ImageFormat.Png);
XP does not support icons in the PNG format, that feature wasn't added until Vista.
You'll need to fallback to the BMP format if XP support is important. Do note that just changing the ImageFormat isn't enough, you'll need to ensure that the pixel format matches what you wrote in the header and you should not write the BITMAPFILEHEADER. In other words, skip the first 14 bytes that the Save() method writes.
Upvotes: 1