Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

Create a thumbnail of a large size image in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (8 votes)
25 Jul 2013CPOL 55.6K   10   8
How to create thumbnail of a large size image in C#.

Introduction

In most projects the user uploads large size images in the application, while on many interfaces he wants to show these images in thumbnail size. This can be achieved by fixing the size of controls where the user is going to show the image but this takes more time to process due to image size being large. If it is an ASP.NET project then downloading of a large image will take too much time. In that case the user needs to save two copies of the image, one in thumbnail size and the other in actual size. The thumbnail image can be achieved by the below given method.

Using the code

The function accepts these parameters:

  1. <lcFilename> as path of large size file.
  2. <lnWidth> as width of required thumbnail.
  3. <lnHeight> as height of required thumbnail.

The function returns a Bitmap object of the changed thumbnail image which you can save on the disk.

C#
//---------------------------------
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
    System.Drawing.Bitmap bmpOut = null;
    try
    {
        Bitmap loBMP = new Bitmap(lcFilename);
        ImageFormat loFormat = loBMP.RawFormat;

        decimal lnRatio;
        int lnNewWidth = 0;
        int lnNewHeight = 0;

        //*** If the image is smaller than a thumbnail just return it
        if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
            return loBMP;

        if (loBMP.Width > loBMP.Height)
        {
            lnRatio = (decimal)lnWidth / loBMP.Width;
            lnNewWidth = lnWidth;
            decimal lnTemp = loBMP.Height * lnRatio;
            lnNewHeight = (int)lnTemp;
        }
        else
        {
            lnRatio = (decimal)lnHeight / loBMP.Height;
            lnNewHeight = lnHeight;
            decimal lnTemp = loBMP.Width * lnRatio;
            lnNewWidth = (int)lnTemp;
        }
        bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
        Graphics g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
        g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

        loBMP.Dispose();
    }
    catch
    {
        return null;
    }

    return bmpOut;
}
//----------------------------------- 

License

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


Written By
Technical Lead E. Soft Technologies
India India
Never try out to sort other's problem. Tell them the way only ........... Let do themselves.

Comments and Discussions

 
QuestionSaving the Thumbnail Pin
Member 1208455224-Oct-15 14:15
Member 1208455224-Oct-15 14:15 
GeneralUsing Bitmap Size parameter Pin
Vamsee Krishna Reddy20-May-15 10:52
Vamsee Krishna Reddy20-May-15 10:52 
C#
Size s = new Size(50,50); 
Bitmap bmp = new Bitmap(Image.FromFile("actual.jpg"),s);
bmp.Save("final.jpg");


This may be much easier. not sure about performance. guessing it would be good i guess.
GeneralMy vote of 4 Pin
Sibeesh Venu5-Aug-14 18:59
professionalSibeesh Venu5-Aug-14 18:59 
QuestionMy vote is 4 Pin
A. Najafzadeh31-May-14 19:18
A. Najafzadeh31-May-14 19:18 
QuestionA couple of questions Pin
Dewey25-Jul-13 14:19
Dewey25-Jul-13 14:19 
SuggestionWhy do you use orientation to determine which dimension to use? Pin
Brian A Stephens25-Jul-13 7:54
professionalBrian A Stephens25-Jul-13 7:54 
GeneralRe: Why do you use orientation to determine which dimension to use? Pin
Ravi Bhavnani25-Jul-13 8:57
professionalRavi Bhavnani25-Jul-13 8:57 
BugReturning original image when resizing isn't required Pin
Ravi Bhavnani25-Jul-13 6:46
professionalRavi Bhavnani25-Jul-13 6:46 

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.