Click here to Skip to main content
15,889,281 members
Articles / Web Development / ASP.NET

Thumbnail using C# .NET

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
7 Sep 2011CPOL2 min read 47.4K   2.2K   17   13
Creating image thumbnail using C# .NET, useful in web applications.

Introduction

This tutorial will show you how to generate image thumbnails dynamically from an original big image.

Background

Many of the web developers will show the big images as small by giving the attribute values for the image tag. This will slow down the webpage loading. For better performance the best method is creating the small thumbnail images for showing small size image on the web pages. But for every upload of the images by the user we cannot create a thumbnail image, it’s a tedious process. I planed to create a method which will create a thumbnail automatically while the image upload and saves in a separate path. This is a light weight process and easy to use. The only thing is you have to add one more filed for saving the thumbnail image path while dealing with databases.

Following are the features of this technique.

  • Good quality.
  • Desired thumbnail size.

Using the Code

Below is the method which will generates the thumbnail and saves to the destination folder.

C#
public void GenerateThumbNail(string sourcefile, string destinationfile,int width)
{
    System.Drawing.Image image = 
       System.Drawing.Image.FromFile(Server.MapPath(sourcefile));
    int srcWidth = image.Width;
    int srcHeight = image.Height;
    int thumbWidth = width;
    int thumbHeight;
    Bitmap bmp;
    if (srcHeight > srcWidth)
    {
        thumbHeight = (srcHeight / srcWidth) * thumbWidth;
        bmp = new Bitmap(thumbWidth, thumbHeight);
    }
    else
    {
        thumbHeight = thumbWidth;
        thumbWidth = (srcWidth / srcHeight) * thumbHeight;
        bmp = new Bitmap(thumbWidth, thumbHeight);
    }

    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    System.Drawing.Rectangle rectDestination = 
           new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
    gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
    bmp.Save(Server.MapPath(destinationfile));
    bmp.Dispose();
    image.Dispose();
}

In the above method we are passing three parameters first one is the soruce location of the image and the second one is the destination location of the image and the third parameter is the width. At first we need to include two namespaces.

C#
using System.Drawing;
using System.Drawing.Drawing2D;

Then create an instance image from the source file as follows

System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(sourcefile));

After getting the image height and width using the instance of the image we need to set the targetted thumbnail image’s height and width. Create and instance for the bitmap. By using the below logic we can get the width and height of the targetted thumbnail. Set the thumbnails’s width and height to the bitmap.

C#
Bitmap bmp;
if (srcHeight > srcWidth)
{
    thumbHeight = (srcHeight / srcWidth) * thumbWidth;
    bmp = new Bitmap(thumbWidth, thumbHeight);
}
else
{
    thumbHeight = thumbWidth;
    thumbWidth = (srcWidth / srcHeight) * thumbHeight;
    bmp = new Bitmap(thumbWidth, thumbHeight);
}

After this for creating the high quality thumbnails create an instance for the Graphics class with the bitmap. Set the SmoothingMode and CompositingQuality to HighQuality and InterpolationMode to Hign. Then draw a rectangle with the thumbnail’s height and width using the rectangle class and place the image inside the rectangle using the DrawImage class. Now the bitmap image i.e., thumbnail image is ready. Now save the thumbnail the the destination location using bmp.save(/*destination location*/) method.

I think mostly this example will help in website development for loading webpages fast with small images instead of loading large image with small sizes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionAnother solution Pin
ZamirF28-Aug-12 10:30
ZamirF28-Aug-12 10:30 
QuestionHTTP Error 400 - Bad Request. Pin
Abdo5863-May-12 14:03
Abdo5863-May-12 14:03 
QuestionIts all good Pin
HaBiX8-Sep-11 23:51
HaBiX8-Sep-11 23:51 
GeneralMy vote of 3 Pin
saxenaabhi67-Sep-11 15:22
saxenaabhi67-Sep-11 15:22 
GeneralRe: My vote of 3 Pin
Dan Mordechay7-Sep-11 22:54
Dan Mordechay7-Sep-11 22:54 
Question"think mostly this example will help in website development for loading webpages fast with small images instead of loading large image with small sizes." Pin
crocks2567-Sep-11 4:37
crocks2567-Sep-11 4:37 
AnswerRe: "think mostly this example will help in website development for loading webpages fast with small images instead of loading large image with small sizes." Pin
supernova56667-Sep-11 5:15
supernova56667-Sep-11 5:15 
Not sure you got the point. I do not think the author is suggesting to resize image everytime the thumbnail is needed. This code could be used in a few ways that would not be as taxing as you suggest.

1: Simply gen. a thumbnail image on upload. Not everyone wants to or even knows how to create 2 images (1 thumb/ 1 fullsize) to upload to a website. This code could easily auto gen. a thumbnail ONLY once when a user uploads content.

2: You could also gen. the thumbnail on a more on-demand basis. Not EVERY TIME, only the first time the content was accessed. Once the thumb is saved you would not have to regen it ever again unless the content changes.

So my point is your both right. Yes it is silly to dynamically gen. a thumb image everytime you display a page. But also the code can be useful for creating thumbs automatically on the fly whether up front when the user uploads it or on the fly the first time its needed.

That being said this sort of thumb gen. code has been around awhile, and is nothing new.
GeneralRe: "think mostly this example will help in website development for loading webpages fast with small images instead of loading large image with small sizes." Pin
R.DileepKumar24-Jan-12 22:54
R.DileepKumar24-Jan-12 22:54 
AnswerRe: "think mostly this example will help in website development for loading webpages fast with small images instead of loading large image with small sizes." Pin
R.DileepKumar7-Sep-11 23:19
R.DileepKumar7-Sep-11 23:19 
GeneralRe: "think mostly this example will help in website development for loading webpages fast with small images instead of loading large image with small sizes." Pin
HaBiX12-Sep-11 22:59
HaBiX12-Sep-11 22:59 
GeneralMy vote of 1 Pin
crocks2567-Sep-11 4:35
crocks2567-Sep-11 4:35 
GeneralRe: My vote of 1 Pin
R.DileepKumar7-Sep-11 23:16
R.DileepKumar7-Sep-11 23:16 
GeneralRe: My vote of 1 Pin
R.DileepKumar13-Sep-11 0:04
R.DileepKumar13-Sep-11 0:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.