Upload image to file system and Create Thumbnail Image on the Fly in C# and ASP.Net
Some times, we require to upload images to file system folder and additionally the thumbnail version of the same image in ASP.Net.
To do this,
Drag a FileUpload(fuImage) control and a button control to upload the image in the ASPX page.
protected void btnUpload_Click(object sender, EventArgs e)
{
string ImgName = fuImage.FileName;
fuImage.SaveAs(Server.MapPath("~").ToString() +"\\"+ ImgName);
Stream imgStream = fuImage.PostedFile.InputStream;
Bitmap bmThumb = new Bitmap(imgStream);
System.Drawing.Image im = bmThumb.GetThumbnailImage(100, 100, null, IntPtr.Zero);
im.Save(Server.MapPath("~").ToString() + "\\ThumbNail_" + ImgName);
}
The above code uses BitMap object to convert the image uploaded to thumbnail image. The GetThubnailImage() method packed with this object takes the dimensions of the thumbnail image as paramter and returns the thumbnail image.
|