在 C# 中从原始像素数据创建的位图不会在 IE 中显示

Bitmap created from raw pixel data in c# won't display in IE

我从位图中提取原始 RGB 像素数据,将其存储在字节数组中,然后从该数组重新创建新位图。它工作正常,图像在 Chrome、Firefox 中正确显示,但在 IE 中不正确。

我假设这是因为我没有写任何 header 或元数据。 Paint 打开文件,但不是 Photoshop 有趣的是,抱怨格式说 "Could not place the document 'Image.bmp' because the file-format module cannot parse the file"。

我不担心 PS,但我确实需要它们在 IE 中显示,尽管几乎可以肯定它们的问题是由相同的因素引起的。

我的问题是缺少什么信息导致 IE 无法显示我的位图,我该如何在 C# 中添加它?

保存位图功能

public void SaveBitmap(byte[] array, int width, int height)
{
    // Create new bitmap
    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);

    // Lock the bitmap for writing
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);

    // Copy the RGB values into to the bitmap
    System.Runtime.InteropServices.Marshal.Copy(array, 0, bmpData.Scan0, array.Length);

    // Unlock the bits and save.
    bmp.UnlockBits(bmpData);
    bmp.Save("Image.bmp");
}

This 是我的代码创建的示例图像。它在一个 zip 文件中,因为有趣的是,如果我将图像上传到 web-server,IE 可以 显示它。就好像服务器对文件执行了某种处理,添加了缺失的 header 信息...

Image.Save(string) saves the image as PNG, so you have created a PNG image with a BMP filename extension. Use Image.Save(string, ImageFormat) 指定格式如下:

bmp.Save("Image.bmp", ImageFormat.Bmp);