Skip navigation

Monthly Archives: March 2009

here is code to create thumbnail of image …. i found some where… useful for asp.net :)

protected void Button1_Click(object sender, EventArgs e)
{
// Get the source image to a System.Drawing.Image object
String src=@”c:\test.jpg”;   //absolute location of source image

String dest=@”c:\test_th.jpg”;    //absolute location of the new image created(thumbnail)

int thumbWidth=40;    //width of the image (thumbnail) to produce

System.Drawing.Image image =  System.Drawing.Image.FromFile(src);

// Create a System.Drawing.Bitmap with the desired width and height of the thumbnail.

int srcWidth=image.Width;
int srcHeight=image.Height;

// we will get the sizeratio in decimal so that we dont get exception in
// case width is less then height.

Decimal sizeRatio = ((Decimal)srcHeight/srcWidth);
int thumbHeight=Decimal.ToInt32(sizeRatio*thumbWidth);
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);

//- Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);

//- Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//- Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

//- Set the System.Drawing.Graphics object property InterpolationMode to High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//- Draw the original image into the target Graphics object scaling to the desired width and height
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

//- Save to destination file
bmp.Save(dest);
//- dispose / release  resources
bmp.Dispose();
image.Dispose();
}

Welcome to http://samols.wordpress.com/

this is just blog… don’t mind it…

Follow

Get every new post delivered to your Inbox.