DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Arthur has posted 13 posts at DZone. View Full User Profile

C#: Resize An Image While Maintaining Aspect Ratio And Maximum Height

  • submit to reddit
        // This allows us to resize the image. It prevents skewed images and 
// also vertically long images caused by trying to maintain the aspect 
// ratio on images who's height is larger than their width

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
	System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

	// Prevent using images internal thumbnail

	if (OnlyResizeIfWider)
		if (FullsizeImage.Width <= NewWidth)
			NewWidth = FullsizeImage.Width;

	int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
	if (NewHeight > MaxHeight)
		// Resize with height instead
		NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
		NewHeight = MaxHeight;

	System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

	// Clear handle to original file so that we can overwrite it if necessary

	// Save resized picture


Snippets Manager replied on Mon, 2011/09/19 - 9:54am

Re: Resizing Image in C# Hi All, Please check out the following links..... Thanks !!!!

Snippets Manager replied on Thu, 2010/06/10 - 1:11pm

I was wondering if someone could help me out. I am creating a flash based website for a friend. I have done all the links and it works great. The only thing is that the images increase as the web browser is maximised. I have the images in a movie clip, and I have named it "pageContainer_mc" I have set the HTML dimensions settings to 100% Width and height as I would like it to decrease as the web browser size is decreased, but to increase to a certain size resolution 900 x 651, and stay that size no matter how big the browser is increased. This is what I am trying to create: Would you be able to help? If anyone knows what that ActionScript code for it, or is able to resolve it for me. I could send you the file if need be. Plus, I tried using the snipet code, and it gave me a syntax error. Is there something I am doing wrong? Thanks.

Snippets Manager replied on Wed, 2009/10/14 - 3:48pm

Well done! Works great.

Snippets Manager replied on Fri, 2009/04/10 - 2:05pm

it s very nice.... worked me perfectly... thanks buddy....

Snippets Manager replied on Wed, 2009/04/08 - 6:48am

Keep in mind that you might be changing the pixel format when doing this. For instance I'm working with 8bpp indexed images and doing a GetThumbnail or even just asking for a new bitmap with a new size but based on my original image will still make it a 32bpp image.

Snippets Manager replied on Mon, 2009/03/02 - 9:52am

Legendary! I've been looking for just this bit of code for a couple of days. My maths is shocking and it was the illusive line below that had me stuck! int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; Nice one, whoever wrote this!

Fede Ram replied on Mon, 2008/09/15 - 8:54pm

Awasome! It worked, and very good :) Thanks for sharing!!