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
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