Resize image from SQL server and display in asp:image

G

Guest

I am storing an image in an SQL database and have one field as an image
datatype. I am also using a webservice to transport data.

I want to be able to resize the image and pass back a thumbnail image in a
byte[] of what is in the database.

I also want to be able to display this image in an <asp:image></asp:image>
control.

Is there any way to do this? Most of the examples that I've seen have used
the technique of having the src of the image set to a separate page with a
querystring value, and have the page do a Response.BinaryWrite(). I don't
want to do it that way becuase that would mean I would have to call the
webservice again, and I only want to call the webservice once.


Thanks in advance,


John Scott.
 
G

Guest

Well,

I sort of solved this one myself....here is what I did [C#]:

first of all I did a query to the database and put the record into an
object. One field in the database is an image data type field and one field
in the object is a byte []. When I pull the record from sql server I store
the image in the object's ImageFile attribute.

I write the object's byte[] into a memory stream.
I use the memory stream to initialze a Bitmap object
I use the GetThumbnailImage method from the Bitmap object to resize the image
Once I have a resized image, I save the image to a new memory stream and
assign the memory stream .ToArray() to the obj.ImageFile attribue and the
byte array gets returned with the new size.

Here's some code:


obj.ImageFile = rs["IMGFILE"];
Byte[] img = obj.ImageFile;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(img, 0, img.Length);//write the image to a memory stream

//call a bitmap constructor using the memory stream
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);

//get a thumbnail of the image from the database.
System.Drawing.Image thumb = bmp.GetThumbnailImage(
95,
95,
new System.Drawing.Bitmap.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);

bmp.Dispose();//dispose of the original bitmap
System.IO.MemoryStream thumbStream = new System.IO.MemoryStream();
//save the image back to the memory stream
thumb.Save(thumbStream,System.Drawing.Imaging.ImageFormat.Gif);

//assign the newly sized image back to the object's byte [] attribute
obj.ImageFile = thumbStream.ToArray();

ms.Close(); //close the memory stream
thumbStream.Close();
 

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

Top