0

I store images of various types in a database and am using a .ashx to retrieve the image and write it to the response outputstream in order to display it on my site.

This works great for any image type EXCEPT bitmaps. All I get for any bitmap I try to store & display is a black box the size of the bitmap.

I know the bitmap is stored correctly in my DB, because if I retrieve it and store it to disk, it works great.

Here's the code in question:

                Image image = GetImage(out type);
                context.Response.ContentType = type;

                // Save the image to the OutputStream
                ImageFormat i_format = null;
                switch (type.ToLower())
                {
                    case "image/jpg":
                    case "image/jpeg":
                        {
                            i_format = ImageFormat.Jpeg;
                        }
                    break;
                    case "image/png":
                        {
                            i_format = ImageFormat.Png;
                        }
                    break;
                    case "image/bmp":
                        {
                            i_format = ImageFormat.Bmp;
                        }
                    break;
                    case "image/gif":
                        {
                            i_format = ImageFormat.Gif;
                        }
                    break;
                }
                //this results in a black box if it's a bitmap.
                //all other image types work fine
                image.Save(context.Response.OutputStream, i_format);

                //if I save the bitmap image to a file, it's fine - no black box
                image.Save("c:\\temp\\test.bmp", i_format);

I have to think it's something obvious I'm doing wrong, but heck if I can figure out what's going on.

EDIT:

I've tracked it down a bit farther. If I right click on the black image that's displayed in the browser and "Save as...", the resulting file is truncated. For example, if the original image is 126 bytes, the saved image is 122 bytes.

The issue isn't the image itself - as I say above, image.Save to a file works fine.

So now I'm thinking there's something that's happening when I'm saving the image to the response.OutputStream that's causing it to be truncated. I've tried setting the content-length to the correct size, and that didn't help.

Still no solution, but I'm getting a bit farther...

BDW
  • 612
  • 1
  • 8
  • 20
  • All looks reasonable... are you DEFINITELY getting the right ContentType value back? – Scrappydog May 29 '12 at 20:40
  • I'm getting back image/bmp. And I've tried this with multiple bmp files, it all looks great. Truly a mystery as to why this isn't working. – BDW May 30 '12 at 17:10

1 Answers1

0

I'm seeing a couple references to "image/x-ms-bmp" being the correct mime type to be using (vs "image/bmp")... give that a try...

Scrappydog
  • 2,864
  • 1
  • 21
  • 23
  • I changed the ContentType to be image/x-ms-bmp, didn't do anything different. There is no Enum for a ImageFormat of that type. – BDW May 29 '12 at 21:47