By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Resize image from SQL server and display in asp:image

P: n/a
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.
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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(Thumb nailCallback),
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.Imag eFormat.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();


"John Scott" wrote:
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.

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.