Introduction
Uploading images & storing them dynamically after resizing is a painless affair now using .NET, unlike what it used to be in Classic ASP. There are several good articles on uploading & resizing images on CodeProject (.NET Image Uploading [^], File Upload with ASP.NET [^], Thumbnail Image Creation and Image format Conversion Utility [^]) & elsewhere on the net (True Image Resizing [^]). One issue with resizing images is that the quality of the image deteriorates. This article is written in praise of the PNG image format & shows how the original quality of the uploaded image of any regular format can be maintained & even be improved.
PNG - What's it?
Check out this excellent article on the PNG image format - FAQ: Converting GIF or JPG to PNG [^]. To paraphrase the article -
- PNG stands for Portable Network Graphics and pronounced "ping".
- PNG is supported by the major browsers (Microsoft IE and Netscape 4.x and higher)
- The images are a lot clearer, no more grainy GIF images or pixelated JPGs!
Source code
This application allows the user to set the dimensions for the image to be resized & uploaded. The resized image is converted into the W3C Portable Network Graphics (PNG) image format by using the PNG
property of the ImageFormat
class.
<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<script runat="server">
void UploadBtn_Click(Object sender, EventArgs e)
{
String UploadedFile = MyFile.PostedFile.FileName;
int ExtractPos = UploadedFile.LastIndexOf("\\") + 1;
String UploadedFileName = UploadedFile.Substring(ExtractPos,UploadedFile.Length - ExtractPos);
FileName.InnerHtml = UploadedFileName;
MyContentType.InnerHtml = MyFile.PostedFile.ContentType;
ContentLength.InnerHtml = MyFile.PostedFile.ContentLength.ToString();
FileDetails.Visible = true;
MyFile.PostedFile.SaveAs(Request.PhysicalApplicationPath
+ "pics\\" + UploadedFileName );
try
{
String imageUrl = UploadedFileName;
int imageHeight =Convert.ToInt32(h.Text);
int imageWidth = Convert.ToInt32(w.Text);
if (imageUrl.IndexOf("/") >= 0 || imageUrl.IndexOf("\\") >= 0 )
{
Response.End();
}
imageUrl = "pics/" + imageUrl;
System.Drawing.Image fullSizeImg
= System.Drawing.Image.FromFile(Server.MapPath(imageUrl));
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack
= new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbNailImg
= fullSizeImg.GetThumbnailImage(imageWidth, imageHeight,
dummyCallBack, IntPtr.Zero);
DateTime MyDate = DateTime.Now;
String MyString = MyDate.ToString("ddMMyyhhmmss") + ".png" ;
thumbNailImg.Save ( Request.PhysicalApplicationPath
+ "pics\\" + MyString , ImageFormat.Png);
thumbNailImg.Dispose();
Image1.AlternateText = "Original image";
Image1.ImageUrl="pics\\" + UploadedFileName;
Image2.AlternateText = "Thumbnail";
Image2.ImageUrl="pics\\" + MyString;
}
catch(Exception ex)
{
Response.Write("An error occurred - " + ex.ToString());
}
}
public bool ThumbnailCallback()
{
return false;
}
</script>
How to run the app
Unzip & run the .aspx file using IIS. Make sure a folder named PICS is created before-hand in the current working directory.
Conclusion
This article demonstrates how the quality of dynamically created images can be maintained & even enhanced by saving them in PNG format.