smart developer’s blog

This is a C# resource library! Free how to’s and best practices…

Generate image thumbnails using ASP.NET(C#)

with 7 comments

You can use the GetThumbnailImage built-in method to generate a thumbnail using this method I created:

public static void CreateThumbnail(string ImageFrom, string ImageTo, int ImageHeight)
{
System.Drawing.Image i = System.Drawing.Image.FromFile(ImageFrom);

i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

System.Drawing.Image th = i.GetThumbnailImage
(
ImageHeight * i.Width / i.Height,
ImageHeight,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero
);
i.Dispose();

EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)80);
ImageCodecInfo ici = GetEncoderInfo(“image/jpeg”);

th.Save(ImageTo, ici, ep);
th.Dispose();
return;
}

private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j) { if(encoders[j].MimeType == mimeType) return encoders[j]; } return null; } public static bool ThumbnailCallback() { return true; } [/sourcecode] If the Image contains an embedded thumbnail image (all digital cameras attach a thumbnail to the big image), this method retrieves the embedded thumbnail and scales it to the requested size. But the problem is that in most of the cases we do not want this because we want to generate thumbnails bigger than 150-200 pixels as embwdded thumbnails have and this will lead in poor quality images for 400-500 pizels or more thumbnail sizes. The workaround is to use this piece of code: [sourcecode language="csharp"] i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); [/sourcecode] By fliping the image twice we get a new image identical with the original one but without the embedded thumbnail. Now the resulted thumbnail is in a very good quality. If you combine this with the cropped image auto-generation you can get a nice method that can generate a cropped thumbnail. 🙂

Advertisements

Written by smartdev

April 9, 2009 at 3:41 pm

Posted in .Net, Programming

Tagged with ,

7 Responses

Subscribe to comments with RSS.

  1. This is the only solution I know (or variations of it).

    Do you know a better one?

    smartdev

    April 9, 2009 at 4:56 pm

  2. thanks

    Jeff

    April 30, 2009 at 2:38 am

  3. error SetHeight, does not exits

    cuagain

    May 29, 2009 at 12:40 pm

    • You are right. You should use ImageHeight instead.
      Sorry… copy/paste/edit….

      smartdev

      May 29, 2009 at 12:47 pm

  4. can u implemnet these functions with example

    how to call them in application for better understanding

    himanshu

    August 6, 2009 at 6:53 am

    • Simply use it like this:

      CreateThumbnail(“C:\\temp\\image.jpg”, “C:\\temp\\image_th.jpg”, 200);

      or

      CreateThumbnail(Server.MapPath(“/images/image.jpg”), Server.MapPath(“/images/image_th.jpg”), 200);

      smartdev

      August 6, 2009 at 10:07 am

  5. nice snippet

    kricksen

    January 7, 2011 at 11:10 am


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

%d bloggers like this: