简体   繁体   中英

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

I'm extracting raw RGB pixel data from a bitmap, storing it in a byte array, and then recreating a new bitmap from that array. It works fine and the images display correctly in Chrome, Firefox, but not in IE.

I'm assuming this is because I'm not writing any headers or metadata. Paint opens the file, but not Photoshop interestingly, complains about the format saying "Could not place the document 'Image.bmp' because the file-format module cannot parse the file".

I'm not concerned about PS, but I do need them to display in IE, although it's almost certain their issues are caused by the same factor.

My question is what absent information is preventing IE from displaying my bitmaps, and how do I add it, in c#?

Save Bitmap Function

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 is an example image created by my code. It is in a zip file because interestingly if I upload the image to a web-server, IE can display it. It's as if the server performs some kind of processing on the file adding missing header information...

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) to specify the format like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM