KARPACH

WEB DEVELOPER BLOG

How to resize an image in .NET or ASP.NET?

public static System.Drawing.Image ResizeImage(System.Drawing.Image sourceImage, int width, int height)
{
    System.Drawing.Image oThumbNail = new Bitmap(sourceImage, width, height);
    Graphics oGraphic = Graphics.FromImage(oThumbNail);
    oGraphic.CompositingQuality = CompositingQuality.HighQuality;
    oGraphic.SmoothingMode = SmoothingMode.HighQuality;
    oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    Rectangle oRectangle = new Rectangle(0, 0, width, height);
    oGraphic.DrawImage(sourceImage, oRectangle);
    return oThumbNail;
}
Posted on March 19, 2008 by

Comments

Posted on 5/20/2009 10:05:10 AM by enam

how we use it. if i send value in function then source image has problem

You can save it:

oThumbNail.Save(...)

or use it as described here:

Server side thumbnail generation

Posted on 4/26/2011 01:36:26 PM by Nathanael Jones

This article lists the pitfalls of using the above approach:

http://nathanaeljones.com/163/20-image-resizing-pitfalls/

Using this as-is in an asp.net app will crash the server.

It’s best to use a library for image processing that handles memory management properly and supports various image formats.

There is a free and open-source module for doing just that:

http://imageresizingin.net/

Nice catch.