<

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

Published on
7,131 Points
4,031 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
Comment
Author:brene
0 Comments

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Join & Write a Comment

Suggested Articles

I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month