Hi,

I'm trying to display an image that's stored in a SQL Server database. The ContentType and binary data are being returned but the image is coming up as a broken image. To test that the data is being retrieved I tried outputting the content type.

Response.Write(adm["Image_MIME_type"].ToString());

It is getting the corect data i.e. "image/pjpeg"

Then I tried Response.write for the binary data returned

Response.Write((byte[])adm["Image_image"]);

It is outputting printing "System.Byte[]"

Am I right in saying the data is being returned from the SPROC?

Here is the code...

if (adm.HasRows)
            {
                
                while (adm.Read())
                {
                 // Response.Write(adm["Image_MIME_type"].ToString());
                 
                        Response.ContentType = adm["Image_MIME_type"].ToString();
                        Response.BinaryWrite((byte[])adm["Image_image"]);  
                }
            }
            else
            {
                Response.Write("Reaer has no rows");

            }
            adm.Close();
            conn.Close();

Any help would be great, thanks!

Load the image into a byte array.
Pass the byte array into a memory stream
Pass the memory stream into a new or existing bitmap.

byte[] b = (byte[])adm["Image_image"];
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
                        stream.Write(b, 0, b.Length);

Bitmap bmp = new Bitmap(stream);

Error checking, null checking, etc needs to be added, left out for simplicity

Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.