473,499 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem displaying a database image on a web form

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
Nov 19 '05 #1
2 2570
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
http://www.mattberther.com
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


Nov 19 '05 #2
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

Matt Berther <mb******@hotmail.com> wrote in
news:22***************************@news.microsoft. com:
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
http://www.mattberther.com

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1906
by: Martin S | last post by:
I have a form that sends data on to another page so that the user can preview it before it is inserted in a database table. The problem is that the form sends both text and possibly an image to...
1
1410
by: Rob Parker | last post by:
I posted this (with a different subjetc line) in the ms.public.access.forms newsgroup a few days ago, but haven't had a single response there. I'm hoping maybe someone here can help. Using...
1
1754
by: Rob Parker | last post by:
I posted this with a different subject line (in two posts - I added additional info in the second) in this newsgroup a few days ago, but haven't had a single response. Hopefully, that was because...
3
1328
by: Irfan Akram | last post by:
Hi Guys, I am having problems displaying an image from the database, that alreday has been uoploaded successfully. The image loads in correctly, but by default it is displayed on the whole...
3
2327
by: CLEAR-RCIC | last post by:
I have several images i want to display in an ASP.Net application. The images are being passed to me in binary format from another application. Is there a good way to write them directly to an...
8
3276
by: Jon Weston | last post by:
I'm setting up an Access2003 database with pictures. I put a bound ole picture ctrl on a form that's source is the table that contains the pictures and follow ALL the directions for embedding a...
4
2168
by: comp.lang.php | last post by:
'll try to explain this as clearly as possible, sorry if it's unclear. You have in your directory /foo 42 images You have in your database metadata for 30 out of those 42 images You have to...
14
2087
by: ashraf02 | last post by:
i used a code from a website that allows you to display images. however everything works fine from storing the image to the database but it does not display the image. the following code is the...
4
1663
by: shahidrasul | last post by:
i display a pic on picture box and then i want to display another pic after some seconds but problem is that first image is not displaying only display 2nd image 1... display first picture from...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.