Monday, May 28, 2012

C# Code To make thumbnails of full size images

Intriduction

  This Code use for to mkae thumbnailspf full size images..  


This is to written in fileupload code behind

if (FileUpload4.FileName != "")
{
string appPath = Request.PhysicalApplicationPath;
path = appPath + "Images\\" + FileUpload4.FileName;
path6 = "Images/Thumbnail/" + FileUpload4.FileName;
FileUpload4.SaveAs(Server.MapPath("../Images/" + FileUpload4.FileName));

System.Drawing.Image ImageToSave = resizeImage(System.Drawing.Image.FromStream(FileUpload4.FileContent), new Size(150, 150));
// supply actual image to the function
ImageToSave.Save(Server.MapPath(System.IO.Path.Combine("../Images/Thumbnail/", FileUpload4.FileName)));
// the function returns a thumbnail which is saved here

}


This is drawing function that is place below above code

private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
namespace to be included
using System.IO;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.Drawing.Drawing2D; 

0 comments:

Post a Comment

Followers