C#: Create thumbnails with better quality

Posted: December 14, 2012 in .Net/C#
Tags: ,

To generate thumbnail in .Net is really easy, inside Image class ,there is one GetThumbnailImage method.

public void createThumbnail(Image originalImage, int thumbWidth, int thumbHeight, string thumbFullPath)
{
    using(FileStream fs = File.Create(thumbFullPath))
    {
        Image newImage = originalImage.GetThumbnailImage(thumbWidth, thumbHeight, new Image.GetThumbnailImageAbort(OnGetThumbnailImageAbort), IntPtr.Zero);
        newImage.Save(fs, originalImage.RawFormat);
        newImage.Dispose();
    }
}
private static bool OnGetThumbnailImageAbort()
{
    return false;
}

But the quanlity of the thumbnail created by the above way is quite low, to create a thumbnail with better quality.

public void createThumbnail(Image originalImage, int thumbWidth, int thumbHeight, string thumbFullPath)
{
    using(FileStream fs = File.Create(thumbFullPath))
    {
         Bitmap newImage = new Bitmap(originalImage, thumbWidth, thumbHeight); 
         Graphics g = Graphics.FromImage(newImage); 
         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; 
         g.DrawImage(image, 0, 0, newImage.Width, newImage.Height); 
         newImage.Save(fs, image.RawFormat); 
         newImage.Dispose();
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s