Wednesday, March 14, 2007

Create High Quality Thumbnails with .NET

By Ivan Uzunov Blog

Creating thumbnails with .NET is really simple task. You have to call only one method Image.GetThumbnailImage(). The problem is that the quality of the created image is really poor. So I’ve started googling around and the result is this GenerateImageThumbnail() function:

public static void GenerateImageThumbnail(Stream streamImage, string sThumbnailImagePath, int nMaxWidth, int nMaxHeight)

{

Image oImage = Image.FromStream(streamImage);

GenerateImageThumbnail(oImage, sThumbnailImagePath, nMaxWidth, nMaxHeight);

}

public static void GenerateImageThumbnail(string sImagePath, string sThumbnailImagePath, int nMaxWidth, int nMaxHeight)

{

Image oImage = Image.FromFile(sImagePath, true);

GenerateImageThumbnail(oImage, sThumbnailImagePath, nMaxWidth, nMaxHeight);

}

public static void GenerateImageThumbnail(Image oImage, string sThumbnailImagePath, int nMaxWidth, int nMaxHeight)

{

float fRatio = 1;

int nWidth = oImage.Width;

int nHeight = oImage.Height;

//calculate the thumb image size if needed

if (oImage.Width > nMaxWidth || oImage.Height > nMaxHeight)

{

if (oImage.Width >= oImage.Height)

{

fRatio = ((float)oImage.Height) / ((float)oImage.Width);

nWidth = nMaxWidth;

nHeight = Convert.ToInt32(nMaxHeight * fRatio);

}

else

{

fRatio = ((float)oImage.Width) / ((float)oImage.Height);

nWidth = Convert.ToInt32(nMaxWidth * fRatio);

nHeight = nMaxHeight;

}

}

//create the thumbnail ans set it’s settings

Image oThumbnail = new Bitmap(nWidth, nHeight);

Graphics oGraphic = Graphics.FromImage(oThumbnail);

oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

oGraphic.SmoothingMode = SmoothingMode.HighQuality;

oGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

oGraphic.CompositingQuality = CompositingQuality.HighQuality;

oGraphic.DrawImage(oImage, 0, 0, nWidth, nHeight);

//save the thumbnail

if (DefineImageType(sThumbnailImagePath) == ImageFormat.Gif)

{

using (oThumbnail)

{

ImageManipulation.OctreeQuantizer quantizer = new ImageManipulation.OctreeQuantizer(255, 8 );

using (Bitmap bmpQuantized = quantizer.Quantize(oThumbnail))

{

bmpQuantized.Save(sThumbnailImagePath, ImageFormat.Gif);

}

}

}

else

{

ImageCodecInfo[] iciInfo = ImageCodecInfo.GetImageEncoders();

EncoderParameters encoderParameters;

encoderParameters = new EncoderParameters(1);

encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

oThumbnail.Save(sThumbnailImagePath, iciInfo[1], encoderParameters);

}

if (oThumbnail != null) { oThumbnail.Dispose(); }

}

Since there is a problem with creating thumbnails from GIF images the function uses a class ImageManipulation.OctreeQuantizer. You can get the source code for this class from this MSDN article “Optimizing Color Quantization for ASP.NET Images” written by Morgan Skinner. You can download the project DotNET_Color_Quantization_Code.msi, build it and use the ImageManipulation.dll in your project. Of course the animated GIFs will not be animated anymore, but at least the transparency is preserved.


You can use the function with this sample code:

GenerateImageThumbnail(filePhoto.PostedFile.InputStream,"c:\your_thumbnaill_file_name.jpg", 400, 400);

1 comment:

Rod said...

In this code you use a function called DefineImageType(). What is its implementation?

Also, there appears to be another type called ImageManipulation. What is its full definition?





Google