SoFunction
Updated on 2025-03-07

Implementation of how to generate thumbnails in C# image processing

Thumbnails usually display the image content in a certain amount, or crop it. There are two main purposes: one is to provide certain preview functions, and the other is to save screen display space and traffic.
In the website, we usually use pictures in the list of products, such as malls, books, news, etc. How do we generate thumbnails in C#? That is, shrink the picture. Let’s take a look at how to shrink the picture.

The method parameters are as follows:

  • originalImagePath: source map path (physical path)
  • thumbnailPath: thumbnail path (physical path)
  • width: Generate thumbnail width
  • height: generate thumbnail height
  • mode: the mode for generating thumbnails

The code is as follows

 
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode mode)
        {
             img_OriginalImage = null;
            ImageFormat tFormat = null;
            try
            {
                img_OriginalImage = (Bitmap)(originalImagePath);
                tFormat = img_OriginalImage.RawFormat;
            }
            catch (Exception ex)
            {
                if (img_OriginalImage != null)
                {
                    img_OriginalImage.Dispose();
                }
               
                throw new Exception("Compressed Image:" + originalImagePath + "Failed!\r\n" + );
 
            }
 
            int i_ToWidth = width;
            int i_ToHeight = height;
 
            int x = 0;
            int y = 0;
 
            int i_OriginalWidth = img_OriginalImage.Width;
            int i_OriginalHeight = img_OriginalImage.Height;
 
            switch (mode)
            {
                case :
                    i_ToHeight = img_OriginalImage.Height * width / img_OriginalImage.Width;
                    break;
                case :
                    i_ToWidth = img_OriginalImage.Width * height / img_OriginalImage.Height;
                    break;
                case :
                    if ((double)img_OriginalImage.Width / (double)img_OriginalImage.Height > (double)i_ToWidth / (double)i_ToHeight)
                    {
                        i_OriginalHeight = img_OriginalImage.Height;
                        i_OriginalWidth = img_OriginalImage.Height * i_ToWidth / i_ToHeight;
                        y = 0;
                        x = (img_OriginalImage.Width - i_OriginalWidth) / 2;
                    }
                    else
                    {
                        i_OriginalWidth = img_OriginalImage.Width;
                        i_OriginalHeight = img_OriginalImage.Width * height / i_ToWidth;
                        x = 0;
                        y = (img_OriginalImage.Height - i_OriginalHeight) / 2;
                    }
                    break;
                default:
                    break;
            }
 
            // Create a new BMP picture             img_BitMap = new (i_ToWidth, i_ToHeight);
            // Create a new drawing board             gp = (img_BitMap);
            //Set high-quality interpolation method             = .;
            //Set high quality and low speed to show smoothness             = .;
             = ;
 
            //Clear the canvas and fill it with transparent background color            ();
            //Specify the location and draw the specified part of the original picture by size            (img_OriginalImage, new Rectangle(0, 0, i_ToWidth, i_ToHeight), new Rectangle(x, y, i_OriginalWidth, i_OriginalHeight), );
 
            try
            {
                EncoderParameters ep = new EncoderParameters();
                long[] qy = new long[1];
                qy[0] = 100;//Set the compression ratio 1-100                EncoderParameter eParam = new EncoderParameter(, qy);
                [0] = eParam;
 
                ImageCodecInfo[] arrayICI = ();
                ImageCodecInfo jpegICIinfo = null;
                for (int i = 0; i < ; i++)
                {
                    if (arrayICI[i].("JPEG"))
                    {
                        jpegICIinfo = arrayICI[i];
                        break;
                    }
                }
                if (jpegICIinfo != null && Equals(tFormat, ))
                {
                    //Save pictures in JPG format                    img_BitMap.Save(thumbnailPath, jpegICIinfo, ep);
                    //++;
                }
                else
                {
                    img_BitMap.Save(thumbnailPath, tFormat);
                    //++;
                }
            }
            catch (Exception ex)
            {
                ("Copy the picture:" + originalImagePath + "Failed!\r\n" +  + );
            }
            finally
            {
                if (img_OriginalImage != null)
                {
                    img_OriginalImage.Dispose();
                }
                if (img_BitMap != null)
                {
                    img_BitMap.Dispose();
                }
                if (gp != null)
                {
                    ();
                }
                //();
            }
        }
public enum ThumbnailMode
    {
        /// <summary>
        /// Specify the width, the height is scaled to scale        /// </summary>
        Width = 0,
 
        /// <summary>
        /// Specify the height, the width is scaled to scale according to the scale        /// </summary>
        Height = 1,
 
        /// <summary>
        /// Cut according to specified height and width        /// </summary>
        Cut = 2,
 
        /// <summary>
        /// Specify the width, the height is scaled to scale        /// </summary>
        None = 3
    }

In this method we first use (Bitmap) to load the file into bitmap.
Calculate the original drawing area according to the compression mode passed in by the mode.
Draw the calculated area to the new BitMap.
Save the drawn BitMap image to the specified file.
The thumbnail generation is completed through the above steps.

This is the end of this article about how to generate thumbnails by C# image processing. For more related content for C# thumbnail generation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!