High resolution thumbnails with dynamic width and height
This code will check the image original height and width and accordingly will create the
thumb by setting its suitable thumb height and width. Will give high resolution thumb and
also image will not be blur or squeeze. Name space needed
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
Path - path of original file where located
Width,height - will be supplied as perameter.
Will create new thumbnail on c:\hello.jpg
System.Drawing.Image img = System.Drawing.Image.FromFile(Path);
int srcWidth = img.Width;
int srcHeight = img.Height;
Decimal sizeRatio = ((Decimal)srcHeight / srcWidth);
height = Decimal.ToInt32(sizeRatio * width);
if (height > 250)
height = 250;
Size sz = new Size(width, height);
using (Bitmap bmp = new Bitmap(Path))
{
using (Bitmap bmpThumb = new Bitmap((System.Drawing.Image)bmp, sz))
{
using (Graphics grp = Graphics.FromImage(bmpThumb))
{
grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
grp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
grp.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
grp.DrawImage(bmp, new Rectangle(0, 0, bmpThumb.Width, bmpThumb.Height));
string strDestPath = "c:\\hello.jpg"; // you can give your path where you want to create file
bmpThumb.Save(strDestPath, codec, eParams);
}
}
}
Showing posts with label Thumbnails. Show all posts
Showing posts with label Thumbnails. Show all posts
Monday, July 21, 2008
Monday, July 7, 2008
Thumbnails-Part2
Need to include following name spaces
using System.Drawing;
using System.Drawing.Drawing2D;
-------------Code-------------
int width=200;
int height=200;
string Path=server.mappath(".") + "\\UploadPhotos\\hello_old.jpg";
System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Path);
Bitmap SmallBitmap = new Bitmap(width, height, OriginalImage.PixelFormat);
Graphics SmallImg = Graphics.FromImage(SmallBitmap);
SmallImg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
SmallImg.DrawImage(OriginalImage, 0, 0, SmallBitmap.Width, SmallBitmap.Height);
//Print to the browser
context.Response.ContentType = "image/jpeg";
SmallBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
string NewFileName = "";
SmallBitmap.Save(server.mappath(".") + ""\\UploadPhotos\\hello.jpg", ImageFormat.Jpeg);
SmallImg.Dispose();
SmallImg.Dispose();
using System.Drawing;
using System.Drawing.Drawing2D;
-------------Code-------------
int width=200;
int height=200;
string Path=server.mappath(".") + "\\UploadPhotos\\hello_old.jpg";
System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Path);
Bitmap SmallBitmap = new Bitmap(width, height, OriginalImage.PixelFormat);
Graphics SmallImg = Graphics.FromImage(SmallBitmap);
SmallImg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
SmallImg.DrawImage(OriginalImage, 0, 0, SmallBitmap.Width, SmallBitmap.Height);
//Print to the browser
context.Response.ContentType = "image/jpeg";
SmallBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
string NewFileName = "";
SmallBitmap.Save(server.mappath(".") + ""\\UploadPhotos\\hello.jpg", ImageFormat.Jpeg);
SmallImg.Dispose();
SmallImg.Dispose();
Subscribe to:
Posts (Atom)