<

Resize an image while maintaining the aspect ratio given a maximum height and/or width

Published on
7,287 Points
4,187 Views
1 Endorsement
Last Modified:
This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned.

This example is designed to be inserted into the Page_Load event of an aspx page and the image is returned as the Response.OutputStream.
To use this:

1


Create a webpage called ShowImage.aspx and put the code below into the Page_Load event

2


Ensure you include a reference to System.Drawing.Imaging (eg. using System.Drawing.Imaging;)

3


Create another webpage calling it whatever you wish

4


Insert an Image with the source set to ShowImage.aspx

 
protected void Page_Load(object sender, EventArgs e)
{
// Read in the width and height
int maxHeight, maxWidth;
string h = Request.QueryString["h"];
string w = Request.QueryString["w"];
if (h == null || h == "")
	maxHeight = Int32.MaxValue;
else
	maxHeight = Int32.Parse(h);
if (w == null || w == "")
	maxWidth= Int32.MaxValue;
else
	maxWidth = Int32.Parse(w);
string imageUrl = Request.QueryString["img"];
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl));
// Do we need to create a thumbnail?
Response.ContentType = "image/gif";
if (fullSizeImg.Height > maxHeight || fullSizeImg.Width > maxWidth)
{
	//resize stuff
	int newWidth, newHeight;
	if (fullSizeImg.Height >= maxHeight && fullSizeImg.Width < maxWidth)
	{
		newHeight = maxHeight;
		newWidth = fullSizeImg.Width * maxHeight / fullSizeImg.Height;
	}
	else if (fullSizeImg.Width >= maxWidth && fullSizeImg.Height < maxHeight)
	{
		newWidth = maxWidth;
		newHeight = fullSizeImg.Height * maxWidth / fullSizeImg.Width;
	}
	else if (fullSizeImg.Width > maxWidth && fullSizeImg.Height > maxHeight)
	{
		if ((fullSizeImg.Height / fullSizeImg.Width) > 0.75)
		{
			newHeight = maxHeight;
			newWidth = fullSizeImg.Width * maxHeight / fullSizeImg.Height;
		}
		else
		{
			newWidth = maxWidth;
			newHeight = fullSizeImg.Height * maxWidth / fullSizeImg.Width;
		}
	}
	else
		throw new Exception("Image resizing not handled properly"); 
	System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
	System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, dummyCallBack, IntPtr.Zero);
	thumbNailImg.Save(Response.OutputStream, ImageFormat.Gif);
	// Clean up / Dispose...
	thumbNailImg.Dispose();
}
else
{
	fullSizeImg.Save(Response.OutputStream, ImageFormat.Gif);
}
// Clean up / Dispose...
fullSizeImg.Dispose();
}

Open in new window

1
Author:brene
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free