SoFunction
Updated on 2025-03-06

C# method to create thumbnails for a large-size picture

This article describes the method of C# to create thumbnails for a large-size image. Share it for your reference. The specific implementation method is as follows:

public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
   bmpOut = null;
  try
  {
    Bitmap loBMP = new Bitmap(lcFilename);
    ImageFormat loFormat = ;
    decimal lnRatio;
    int lnNewWidth = 0;
    int lnNewHeight = 0;
    //*** If the image is smaller than a thumbnail just return it
    if ( < lnWidth &&  < lnHeight)
      return loBMP;
    if ( > )
    {
      lnRatio = (decimal)lnWidth / ;
      lnNewWidth = lnWidth;
      decimal lnTemp =  * lnRatio;
      lnNewHeight = (int)lnTemp;
    }
    else
    {
      lnRatio = (decimal)lnHeight / ;
      lnNewHeight = lnHeight;
      decimal lnTemp =  * lnRatio;
      lnNewWidth = (int)lnTemp;
    }
    bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
    Graphics g = (bmpOut);
     = .;
    (, 0, 0, lnNewWidth, lnNewHeight);
    (loBMP, 0, 0, lnNewWidth, lnNewHeight);
    ();
  }
  catch
  {
    return null;
  }
  return bmpOut;
}

I hope this article will be helpful to everyone's C# programming.