Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Create a thumbnail of a large size image in C#

, 25 Jul 2013
Rate this:
Please Sign up or sign in to vote.
How to create thumbnail of a large size image in C#.


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.

public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
    System.Drawing.Bitmap bmpOut = null;
        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;
            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);

        return null;

    return bmpOut;


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


About the Author

Kumar Pankaj Verma
Technical Lead E. Soft Technologies
India India
Never try out to sort other's problem. Tell them the way only ........... Let do themselves.
Follow on   Google+

Comments and Discussions

GeneralMy vote of 4 PinprofessionalSibeesh Venu5-Aug-14 18:59 
QuestionMy vote is 4 PinmemberA. Najafzadeh31-May-14 19:18 
QuestionA couple of questions PinmemberDewey25-Jul-13 14:19 
SuggestionWhy do you use orientation to determine which dimension to use? PinprofessionalBrian A Stephens25-Jul-13 7:54 
GeneralRe: Why do you use orientation to determine which dimension to use? PinprofessionalRavi Bhavnani25-Jul-13 8:57 
BugReturning original image when resizing isn't required PinprofessionalRavi Bhavnani25-Jul-13 6:46 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 25 Jul 2013
Article Copyright 2013 by Kumar Pankaj Verma
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid