Create Image Thumbnail on the fly
While working on a Photo gallery project, i realized the power of .NET to enable the developer to create thumbnail images in few lines of code. This gave me the advantage to load my webforms without waiting the whole image to be download to the user. Working with big size images is considered a real pain specially when using a slow internet connection. So let's jump directly to the code.
// Get the path of the original Image
string displayedImg = Server.MapPath("~") + "/Testing.jpg";
// Get the path of the Thumb folder
string displayedImgThumb = Server.MapPath("~") + "/Thumb/";
// Get the original image file name
string imgFileName = System.IO.Path.GetFileName(displayedImg);
// Load original image
System.Drawing.Image myimg = System.Drawing.Image.FromFile(displayedImg);
// Get the thumbnail 100 X 100 px
myimg = myimg.GetThumbnailImage(100, 100, null, IntPtr.Zero);
// Save the new thumbnail image
myimg.Save(displayedImgThumb + imgFileName, myimg.RawFormat);
Best Regards,
HC