Geek Noise
Rants, rambles, news and notes by Peter Provost
29

Resize Image in C#

Thursday, 29 May 2003 16:26 by Peter Provost

Someone asked me to post some code to resize an image. Assuming you know how to load one into a Bitmap, the following method will do it for you.

[UPDATE 2003-05-30 10:27PM] Jeff Lewis correctly pointed out that I forgot a couple of Dispose() calls. Oops. I have corrected the code below. Thanks Jeff.

[UPDATE 2004-08-10] Oops. Still not quite right. It is fixed now. But remember, the caller needs to Dispose() the returned Bitmap.

public Bitmap ResizeBitmap( Bitmap b, int nWidth, int nHeight )
{
Bitmap result = new Bitmap( nWidth, nHeight );
using( Graphics g = Graphics.FromImage( (Image) result ) )
g.DrawImage( b, 0, 0, nWidth, nHeight );
return result;
}

Enjoy!

Currently rated 3.6 by 33 people

  • Currently 3.575758/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   Technology
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed
Comments are closed