Friday, July 9, 2010

Image Thumbnails Nice Code

using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;



string photoPath = Server.MapPath("1.jpg");
int thumbnailSize = 50;
Bitmap photo = new Bitmap(photoPath);
int width, height;
if (photo.Width > photo.Height)
{
width = thumbnailSize;
height = photo.Height * thumbnailSize / photo.Width;
}
else
{
width = photo.Width * thumbnailSize / photo.Height;
height = thumbnailSize;
}
Bitmap target = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(target))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(photo, 0, 0, width, height);
target.Save(Server.MapPath("/2.jpg"));
img.ImageUrl = "~/2.jpg";
}

No comments:

Post a Comment