SoFunction
Updated on 2025-03-06

C# Create thumbnail operation class instance

This article describes the creation of thumbnail operation classes in C#. Share it for your reference. The specific analysis is as follows:

This C# class can generate various forms of thumbnails, can automatically maintain image proportions and thumbnails, and can obtain image sizes based on percentages, etc.

using System;
using ;
using ;
using ;
namespace HtmlSnap
{
 public static class ImageHelper
 {
  /// <summary>
  /// Get thumbnail  /// </summary>
  /// <param name="image"></param>
  /// <param name="width"></param>
  /// <param name="height"></param>
  /// <returns></returns>
  public static Image GetThumbnailImage(Image image, int width, int height)
  {
   if (image == null || width < 1 || height < 1)
    return null;
   // Create a new bmp image   //
   Image bitmap = new (width, height);
   // Create a new drawing board   //
   using (Graphics g = (bitmap))
   {
    // Set high-quality interpolation method    //
     = .;
    // Set high quality and low speed to show smoothness    //
     = .;
    // High-quality, low-speed composite    //
     = .;
    // Clear the canvas and fill it with transparent background color    //
    ();
    // Draw the specified part of the original picture at the specified location and by the specified size    //
    (image, new Rectangle(0, 0, width, height),
     new Rectangle(0, 0, , ),
     );
    return bitmap;
   }
  }
  /// <summary>
  /// Generate thumbnails and maintain aspect ratio  /// </summary>
  /// <param name="image"></param>
  /// <param name="width"></param>
  /// <param name="height"></param>
  /// <returns>Object after generating thumbnails</returns>  public static Image GetThumbnailImageKeepRatio(Image image, int width, int height)
  {
   Size imageSize = GetImageSize(image, width, height);
   return GetThumbnailImage(image, , );
  }
  /// &lt;summary&gt;
  /// Get the size of the picture according to the percentage  /// &lt;/summary&gt;
  /// &lt;param name="picture"&gt;&lt;/param&gt;
  /// &lt;param name="percent"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static Size GetImageSize(Image picture, int percent)
  {
   if (picture == null || percent &lt; 1)
    return ;
   int width =  * percent / 100;
   int height =  * percent / 100;
   return GetImageSize(picture, width, height);
  }
  /// &lt;summary&gt;
  /// Return the size of the picture according to the set size, considering the ratio of the length and width of the picture  /// &lt;/summary&gt;
  /// &lt;param name="picture"&gt;&lt;/param&gt;
  /// &lt;param name="width"&gt;&lt;/param&gt;
  /// &lt;param name="height"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static Size GetImageSize(Image picture, int width, int height)
  {
   if (picture == null || width &lt; 1 || height &lt; 1)
    return ;
   Size imageSize;
   imageSize = new Size(width, height);
   double heightRatio = (double) / ;
   double widthRatio = (double) / ;
   int desiredHeight = ;
   int desiredWidth = ;
 
    = desiredHeight;
   if (widthRatio &gt; 0)
     = Convert.ToInt32( * widthRatio);
   if ( &gt; desiredWidth)
   {
     = desiredWidth;
     = Convert.ToInt32( * heightRatio);
   }
   return imageSize;
  }
 
  /// &lt;summary&gt;
  /// Get all relevant information about the image encoding codec  /// &lt;/summary&gt;
  /// <param name="mimeType">Stands of type of multipurpose Internet Mail Extension Protocol (MIME) that contains encoding and decoder</param>  /// <returns>Return all relevant information about the image encoding codec</returns>  public static ImageCodecInfo GetCodecInfo(string mimeType)
  {
   ImageCodecInfo[] CodecInfo = ();
   foreach (ImageCodecInfo ici in CodecInfo)
   {
    if ( == mimeType) return ici;
   }
   return null;
  }
  public static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
  {
   ImageCodecInfo[] encoders = ();
   foreach (ImageCodecInfo icf in encoders)
   {
    if ( == )
    {
     return icf;
    }
   }
   return null;
  }
  public static void SaveImage(Image image, string savePath, ImageFormat format)
  {
   SaveImage(image, savePath, GetImageCodecInfo(format));
  }
  /// &lt;summary&gt;
  /// Save pictures with high quality  /// &lt;/summary&gt;
  /// &lt;param name="image"&gt;&lt;/param&gt;
  /// &lt;param name="savePath"&gt;&lt;/param&gt;
  /// &lt;param name="ici"&gt;&lt;/param&gt;
  private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
  {
   // Set the EncoderParameters object of the original image object   //
   EncoderParameters parms = new EncoderParameters(1);
   EncoderParameter parm = new EncoderParameter(, ((long)95));
   [0] = parm;
   (savePath, ici, parms);
   ();
  }
 }
}

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