Problem displaying a database image on a web form

J

Jeronimo Bertran

Hello,

I am using a rendering aspx page to display a database image on an Image
control. When the database record is retrieved, I am saving the bitmap
to a session variable for the rendering aspx to load. Here is the code
on the rendering page:

CODE FOR ImageForm.aspx

private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
{
Bitmap b = (Bitmap) Session["ImageForm"];
Response.Clear();

if (b != null)
{
try
{
Response.ContentType = "image/jpeg";
b.Save(Response.OutputStream, ImageFormat.Jpeg);
}
catch(Exception ex)
{
string s = ex.Message;
}
b.Dispose();
Session["ImageForm"] = null;
}
Response.End();
}
}

Now, this works fine if at the main form I load the image from a file
and save it to the Session variable. The Photo variable is the Image
control:

CODE FOR WebForm.aspx

private void Page_Load(object sender, System.EventArgs e)
{
Bitmap b = null;
b = new Bitmap("c:\\testimage.jpg");
Session["ImageForm"] = b;
Photo.ImageUrl = "ImageForm.aspx";
}

So the above works fine. Now, in order to retrieve the bitmap from an
Image field in an SQL database I am doing the following:

private void Page_Load(object sender, System.EventArgs e)
{
Bitmap b = null;

byte[] img = (byte[]) Row["Image"];

// Perform the conversion
MemoryStream ms = new MemoryStream();
ms.Write(img, 0, img.Length);
b = new Bitmap(ms);
Session["ImageForm"] = b;
ms.Close();
Photo.ImageUrl = "ImageForm.aspx";
}

But this fails on the call to b.Save(Response.OutputStream,
ImageFormat.Jpeg) inside ImageForm.aspx. Although it fails, it is not
trapped by the try catch that I placed there.

The strange thing is that the file that I used in the first example (c:
\\testimage.jpg) was actually generated by obtaining the byte[] from the
database field, calling the ms.Write and saving the resulting bitmap on
a Windows Forms application. I even compared the contents of the byte[]
in each case and found them to be identical. Any idea what might be
wrong??

Thanks,

Jeronimo Bertran
 
M

Matt Berther

Hello Jeronimo,

So... I think the reason this is happening is because you're writing to the
stream, but then not reseting the stream pointer.

Probably the best way to get around this is:

private void Page_Load(object sender, System.EventArgs e)
{
byte[] img = (byte[]) Row["Image"];
MemoryStream ms = new MemoryStream(img);
Bitmap b = new Bitmap(ms);
Session["ImageForm"] = b;

b.Save(Response.OutputStream, ImageFormat.Jpeg);
}
 
J

Jeronimo Bertran

Matt,

Thanks.... I am not saving onto the OutputStream on the same aspx (actually
that is why I am saving it to a session variable)... anyway, I removed the
MemoryStream Close call and the code works. I am not sure if this is very
good programming practice!.

Thanks again.

Jeronimo Bertran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top