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

标签 c# internet-explorer bitmap header metadata

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

我假设这是因为我没有编写任何标题或元数据。 Paint 打开该文件,但有趣的是 Photoshop 却打不开,它提示格式“无法放置文档‘Image.bmp’,因为文件格式模块无法解析该文件”。

我不关心 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 文件中,因为有趣的是,如果我将图像上传到网络服务器,IE可以显示它。就好像服务器对文件执行某种处理,添加丢失的 header 信息......

最佳答案

Image.Save(string)将图像另存为 PNG,因此您已经创建了带有 BMP 文件扩展名的 PNG 图像。使用Image.Save(string, ImageFormat)指定格式如下:

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

关于c# - 从 C# 中的原始像素数据创建的位图不会在 IE 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33841980/

相关文章:

c# - 结构构造函数 : "fields must be fully assigned before control is returned to the caller."

java - 在 Canvas 上绘制滚动曲线路径的最佳方法

android - 如何在 Xamarin Imageview 中显示 MySQL BLOB 图像?

C# 字典 : Multiple KEYS per Value

c# - Interlocked.CompareExchange 是否使用内存屏障?

javascript - 事件在 Firefox/Chrome 中有效,但在 IE 中失败

css - 如何修复 IE 和 Chrome 中 ajax 弹出窗口层上的 YouTube flash 视频叠加问题?

css - Internet Explorer margin 自动不起作用

Android:位图、软引用和 OOM?

c# - SignalR Core 未在 Azure App Service 中使用 Websockets