Hi Guys today I am going to show you how you can generate Thumbnail’s of any selected image dynamically by a C# class. This class is very handy in admin area development, where admin has to upload the images.
By this class you can generate not only Thumbnail but the real resource will also get saved in Servers folder which is handy to show the related big image as well.
We can use this Thumbnail Generator class by just calling the function GenerateThumbNail() and passing the required signatures (Parameters), we need to initiate the class first by Creating the instance first for that below is the code block:
public Helper hlp { get; set; }
Let say we are having a ASP FileUpload control and ASP Button control to select desired file and send the same to server for making thumbnail of selected image.
Below is the *.aspx code:
<fieldset>
<legend>Thumbnail Generator</legend>
<asp:FileUpload ID="flUpload" runat="server" /><asp:Button ID="btnUpload"
runat="server" Text="Upload" onclick="btnUpload_Click" />
</fieldset>
it will generate a html page like below:
Now our Fileupload control having the Sorce file after selecting Image file from local Server (User Computer).
On Upload Button Click by user below mentioned code required:
protected void btnUpload_Click(object sender, EventArgs e)
{
hlp = new Helper();
hlp.GenerateThumbNail(flUpload.PostedFile, "thumbFile",300);
}
Below is the required Class for Generating Thumbnail:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
/// <summary>
/// Summary description for Helper
/// </summary>
public class Helper
{
public Helper()
{
//
// TODO: Add constructor logic here
//
}
public void GenerateThumbNail(HttpPostedFile srcFile,string thumbNailFolder,int width)
{
HttpPostedFile myFile = srcFile;
string filename = srcFile.FileName;
int oFileLength = myFile.ContentLength;
if (oFileLength != 0)
{
int i = filename.LastIndexOf("\\");
filename = filename.Substring(i + 1);
filename = DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + "-" + filename;
string thumb_filename = "thumb_"+DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + "-" + filename;
byte[] myData = new Byte[oFileLength];
myFile.InputStream.Read(myData, 0, oFileLength);
System.IO.FileStream newFile
= new System.IO.FileStream(HttpContext.Current.Server.MapPath("~/images/") + filename,
System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
System.Drawing.Image image =
System.Drawing.Image.FromFile(newFile.Name);
int srcWidth = image.Width;
int srcHeight = image.Height;
int thumbWidth = width;
int thumbHeight;
Bitmap bmp;
if (srcHeight > srcWidth)
{
thumbHeight = (srcHeight / srcWidth) * thumbWidth;
bmp = new Bitmap(thumbWidth, thumbHeight);
}
else
{
thumbHeight = thumbWidth;
thumbWidth = (srcWidth / srcHeight) * thumbHeight;
bmp = new Bitmap(thumbWidth, thumbHeight);
}
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination =
new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
bmp.Save(HttpContext.Current.Server.MapPath("~/"+thumbNailFolder + "/" + thumb_filename));
bmp.Dispose();
image.Dispose();
}
}
}
And that’s it, now be sure to make two folders in you solution (Project). One for original Picture Upload and another one for Thumbnail file.
I am pasting Overall screen shot of Project which will help you :
Cheers…..
Regards,
Anand Tiwari