SoFunction
Updated on 2025-03-07

C# Generate high-quality thumbnail program—the ultimate algorithm

Let's look at the code first:
using System;
using ;
using ;
using .Drawing2D;
/**//// <summary>
/// 
///**Program of high-quality thumbnails**
/// 
///  File: 
/// 
///   Author: Zxjay Piaoyao)
/// 
///  E-Mail: tda7264@
/// 
///  Date: 07-04-07
/// 
/// </summary>
public class GenerateThumbnail
...{
    /**//// <summary>
///    Generate thumbnails Static method
    /// </summary>
/// <param name="pathImageFrom"> Path of the source map (including file name and extension) </param>
/// <param name="pathImageTo"> The path saved by the generated thumbnail (including file name and extension)
Note: The extension must correspond to the generated thumbnail format.</param>
/// <param name="width"> The width of the thumbnail to be generated (pixel value) </param>
/// <param name="height"> The height of the thumbnail "Canvas" (pixel value) </param>
    public static void GenThumbnail(string pathImageFrom,string pathImageTo,int width,int height)
    ...{
        Image imageFrom = null;
        try
        ...{
            imageFrom = (pathImageFrom);
        }
        catch
        ...{
            //throw;
        }
        if (imageFrom == null)
        ...{
            return;
        }        
// Source map width and height
        int imageFromWidth = ;
        int imageFromHeight = ;
// The actual width and height of the generated thumbnail
        int bitmapWidth = width;
        int bitmapHeight = height;
// The position of the generated thumbnail on the above "canvas"
        int X = 0;
        int Y = 0;
// Calculate the actual size of the thumbnail and its position on the "canvas" based on the source image and the thumbnail size to be generated
        if (bitmapHeight * imageFromWidth > bitmapWidth * imageFromHeight)
        ...{
            bitmapHeight = imageFromHeight * width / imageFromWidth;
            Y = (height - bitmapHeight) / 2;
        }
        else
        ...{
            bitmapWidth = imageFromWidth * height / imageFromHeight;
            X = (width - bitmapWidth) / 2;
        }
// Create canvas
        Bitmap bmp = new Bitmap(width, height);
        Graphics g = (bmp);
// Clear with white
        ();
// Specify high-quality bicubital interpolation method. Perform pre-screening to ensure high-quality shrinkage. This mode produces the highest quality converted images.
         = ;
//Specify high-quality and low-speed presentation.
         = ;
//Draw the specified part of the specified Image at the specified location and at the specified size.
        (imageFrom, new Rectangle(X, Y, bitmapWidth, bitmapHeight), new Rectangle(0, 0, imageFromWidth, imageFromHeight), );
        try
        ...{
//After testing, .jpg, format thumbnail size and quality, is the best
            (pathImageTo, );
        }
        catch
        ...{
        }
        finally
        ...{
//Show free resources
            ();
            ();
            ();
        }
    }
}
The generated thumbnail size is certain, without clipping or deformation.
You can test the size and visual quality of various graphic formats, graphic quality, and presentation methods.
After testing: Vista original default desktop.jpg format size: 1024*768,
Generate thumbnails of the original size, and compare them as follows:
Original image.jpg format, 223 KB
.jpg  102KB
.png  1816 KB
.gif  228 KB 
.tiff  2000KB +

Visually, except for the poor quality of .gif, the others are indistinguishable from the source image with the naked eye (I am a little myopic^-^)
Taking into account factors such as patents and versatility, it is recommended to use the .jpg format.