Problem displaying a database image on a web form

Discussion in 'ASP .Net' started by Jeronimo Bertran, Feb 18, 2005.

  1. 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
     
    Jeronimo Bertran, Feb 18, 2005
    #1
    1. Advertisements

  2. Jeronimo Bertran

    Matt Berther Guest

    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);
    }
     
    Matt Berther, Feb 18, 2005
    #2
    1. Advertisements

  3. 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
     
    Jeronimo Bertran, Feb 18, 2005
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.