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();
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)