Resizing images on the server side (ASP.NET)

Posted on December 31, 2006. Filed under: ASP.NET, Tips and Tricks |

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.

Technorati tags: , , ,

Make a Comment

Make A Comment: ( 2 so far )

blockquote and a tags work here.

2 Responses to “Resizing images on the server side (ASP.NET)”

RSS Feed for WinDev – Everything Windows Development Comments RSS Feed

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?

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.


Where's The Comment Form?

  •  

    December 2006
    M T W T F S S
        Jan »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Del.icio.us

  • Subscribe

    Subscribe in NewsGator Online

    Subscribe in Rojo

    Add WinDev to Newsburst from CNET News.com

    Add to My AOL

    Add to netvibes

    Subscribe in Bloglines

    Subscribe in NewsAlloy

    Add WinDev to ODEO

    Subscribe in podnova

    Add to Pageflakes

  • Spams Blocked

Liked it here?
Why not try sites on the blogroll...