<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

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

Published on
7,184 Points
4,084 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

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Join & Write a Comment

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…
I previously published an Experts Exchange video Micro Tutorial that describes how to scan documents to a PDF file using an excellent, free product called Foxit Reader: How to scan to a PDF file with free software (https://www.experts-exchange.co…
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month