Feed on
Posts
Comments

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(); }
    }

public static ImageFormat DefineImageType(string sFileName)
    {
        string sFileExtention = Path.GetExtension(sFileName);
        ImageFormat oImageFormatToReturn;

        switch (sFileExtention)
        {
            case ".gif":
                oImageFormatToReturn = ImageFormat.Gif;
                break;
            case ".png":
                oImageFormatToReturn = ImageFormat.Png;
                break;
            case ".bmp":
                //oImageFormatToReturn = ImageFormat.Bmp;
                //break;
            case ".jpg":
            case ".jpeg":
            case ".jpe":
            default:
                oImageFormatToReturn = ImageFormat.Jpeg;
                break;

        }

        return oImageFormatToReturn;
    }

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);

, , ,
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • dzone
  • Netscape
  • digg
  • YahooMyWeb
  • Technorati

7 Responses to “Create High Quality Thumbnails with .NET”

  1. […] March 13th, 2007 I came across this post on how to Create High Quality Thumbnails with .NET. Since we are currently starting a project that will require this type of functionality, I thought I would post the link here for easy retreival. Posted by Brian Russell Filed in Programming […]

  2. […] Ivan Uzunov Blog » Blog Archive » Create High Quality Thumbnails with .NET –> Comments » […]

  3. on 09 May 2007 at 10:43 am chris

    Thanks very mutch for the code above. I searched exactly this code snipped, to convert gif images with transparent color, it works perfect. The only thing is, some created thumbs will be bigger than the origin image?

  4. on 31 May 2007 at 8:50 pm Gabriel

    Hey Ivan.

    Me (and some others, for what i’ve read googling) wonder if you could post the code (or email it to me) of the DefineImageType function, that you used on your article about generating high resolution thumbnails with asp.net

    I couldnt find it on the article, your site or anywhere..

    Could you please post it? Thanks in advance, and great articles, i just added your blog to my favorites.

    keep up the great work.

    Gabriel

  5. on 02 Jun 2007 at 6:27 pm Ivan Uzunov

    Sorry, I’ve missed to add it. Here it is (added in the post too):

    public static ImageFormat DefineImageType(string sFileName)
    {
    string sFileExtention = Path.GetExtension(sFileName);
    ImageFormat oImageFormatToReturn;

    switch (sFileExtention)
    {
    case “.gif”:
    oImageFormatToReturn = ImageFormat.Gif;
    break;
    case “.png”:
    oImageFormatToReturn = ImageFormat.Png;
    break;
    case “.bmp”:
    //oImageFormatToReturn = ImageFormat.Bmp;
    //break;
    case “.jpg”:
    case “.jpeg”:
    case “.jpe”:
    default:
    oImageFormatToReturn = ImageFormat.Jpeg;
    break;

    }

    return oImageFormatToReturn;
    }

  6. on 25 Aug 2007 at 1:34 pm Ryan

    Just what I needed, Thanks a million..

  7. on 18 Sep 2007 at 9:56 pm Matheus

    This is perfect! Many Thanks!

Trackback URI | Comments RSS

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a