How to resize 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 Wednesday, March 19, 2008 by | Comments (2) | Add Comment

Comments

Gravatar

Re:How to resize image in .NET or ASP.NET?

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

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

You can save it:

oThumbNail.Save(...)

or use it as described here:

http://www.karpach.com/Server-side-thumbnail-generation.htm

Gravatar

Re:How to resize image in .NET or ASP.NET?

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/

Posted on 4/26/2011 1:36:26 PM by Nathanael Jones #
Gravatar

Nice catch.

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5