C# 在windows xp下从位图创建图标

C# 在windows xp下从位图创建图标,c#,bitmap,icons,gdi+,C#,Bitmap,Icons,Gdi+,我在windows xp下创建图标时遇到问题。 这是我的密码: void SaveAsIcon(Bitmap SourceBitmap, string FilePath) { FileStream FS = new FileStream(FilePath, FileMode.Create); // ICO header FS.WriteByte(0); FS.WriteByte(0); FS.Wri

我在windows xp下创建图标时遇到问题。 这是我的密码:

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();
}
它在vista和更高版本下工作正常

我不知道在windows xp下从位图创建图标的方法应该更改什么

当我尝试将图标与上述方法一起使用时,出现以下错误:

生成Win32资源时出错:读取图标时出错

在vista、7和8下,它可以工作

  SourceBitmap.Save(FS, ImageFormat.Png);
XP不支持PNG格式的图标,直到Vista才添加该功能


如果XP支持很重要,您需要回退到BMP格式。请注意,仅更改ImageFormat是不够的,您需要确保像素格式与您在标题中写入的内容匹配,并且不应写入BITMAPFILEHEADER。换句话说,跳过Save()方法写入的前14个字节。

这个错误是从哪里来的?我认为图标应该是ico文件,我在c#的动态编译过程中遇到了这个错误。CompilerParameters cp=新的CompilerParameters();cp.CompilerOptions=“/optimize/warn:4/target:winexe/win32icon:icon.ico”;你使我的回答无效。这样不行,请再次单击“提问”按钮。