Resizing images on the server side (ASP.NET)
Let’s say, you have images (blob) stored in your database. Now you want to resize and generate a thumbnail out of it before you can send it back on the Response stream to be displayed on the page. Here is a way to resize the original image.
- Read the image (blob) into a byte array
byte[] image = null;
while (dr.Read())
{
image = (byte[])dr.GetValue(0);
}
dr.Close()
- Convert the byte array into Bitmap
Bitmap b = (Bitmap)Bitmap.FromStream( new MemoryStream(image));
- Resize the Bitmap
Bitmap output = new Bitmap(b, new Size(320, 240);
Once resized you can save it as an external file or you can attach it with the Response.OutputStream.










Very useful article, just what I was looking for and saved me loads of time! Just one thing
Is there a right bracket ‘)’ missing at the end of the ‘Resize the Bitmap’ line to close the Bitmap() statement?
Mat
February 13, 2007
I’m glad that it was useful to you, Mat. Yes, there is a right bracket missing. Sorry for the typo as I was formatting the code after I copied the code from the editor. And thanks for letting us know.
askars
February 13, 2007