C # generate a high-quality thumbnail Program-Ultimate Algorithm
Source: Internet
Author: User
First look at the Code:
Using System;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Drawing. Drawing2D;
/** // <Summary>
///
/// ** Generate a high-quality thumbnail program **
///
/// File: GenerateThumbnail. cs
///
/// Author: Zhou Zhenxing (Zxjay Piao)
///
// E-Mail: tda7264@163.com
///
/// Date: 07-04-07
///
/// </Summary>
Public class GenerateThumbnail
...{
/** // <Summary>
/// Static method for generating thumbnails
/// </Summary>
/// <Param name = "pathImageFrom"> source image path (including file name and extension) </param>
/// <Param name = "pathImageTo"> path (including file name and extension) of the generated thumbnail)
/// Note: the extension must correspond to the generated thumbnail format </param>
/// <Param name = "width"> width (pixel value) of the thumbnail canvas to be generated </param>
/// <Param name = "height"> height (pixel value) of the thumbnail "canvas" to be generated </param>
Public static void GenThumbnail (string pathImageFrom, string pathImageTo, int width, int height)
...{
Image imageFrom = null;
Try
...{
ImageFrom = Image. FromFile (pathImageFrom );
}
Catch
...{
// Throw;
}
If (imageFrom = null)
...{
Return;
}
// Source image width and height
Int imageFromWidth = imageFrom. Width;
Int imageFromHeight = imageFrom. Height;
// The actual width and height of the generated thumbnail
Int bitmapWidth = width;
Int bitmapHeight = height;
// Position of the generated thumbnail on the 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 size of the thumbnail 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 a canvas
Bitmap bmp = new Bitmap (width, height );
Graphics g = Graphics. FromImage (bmp );
// Clear with white
G. Clear (Color. White );
// Specify a high quality double-cubic Interpolation Method. Perform pre-filtering to ensure high-quality shrinkage. This mode produces the highest quality conversion images.
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
// Specify high quality and low-speed rendering.
G. SmoothingMode = SmoothingMode. HighQuality;
// Draw the specified part of the specified Image at the specified position and in the specified size.
G. DrawImage (imageFrom, new Rectangle (X, Y, bitmapWidth, bitmapHeight), new Rectangle (0, 0, imageFromWidth, imageFromHeight), GraphicsUnit. Pixel );
Try
...{
// The size and quality of. jpg thumbnails are optimized.
Bmp. Save (pathImageTo, ImageFormat. Jpeg );
}
Catch
...{
}
Finally
...{
// Display the resources released
ImageFrom. Dispose ();
Bmp. Dispose ();
G. Dispose ();
}
}
}
The generated thumbnail is of a certain size, with no cropping or deformation.
You can test the size and visual quality of thumbnails generated by various image formats, image quality, and rendering methods.
Tested: Vista original default desktop. jpg size: 1024*768,
Generate a thumbnail of the original size and compare it as follows:
Source .jpg format, 223 KB
. Jpg 102KB
. Png 1816 KB
. Gif 228 KB
. Tiff more than KB
...
Visually, except for the poor. gif quality, other images cannot be different from the source image to the naked eye (I am a little nearsighted ^-^)
The. jpg format is recommended for patents and versatility.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.