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, , ); } /// <summary> /// Get the size of the picture according to the percentage /// </summary> /// <param name="picture"></param> /// <param name="percent"></param> /// <returns></returns> public static Size GetImageSize(Image picture, int percent) { if (picture == null || percent < 1) return ; int width = * percent / 100; int height = * percent / 100; return GetImageSize(picture, width, height); } /// <summary> /// Return the size of the picture according to the set size, considering the ratio of the length and width of the picture /// </summary> /// <param name="picture"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Size GetImageSize(Image picture, int width, int height) { if (picture == null || width < 1 || height < 1) return ; Size imageSize; imageSize = new Size(width, height); double heightRatio = (double) / ; double widthRatio = (double) / ; int desiredHeight = ; int desiredWidth = ; = desiredHeight; if (widthRatio > 0) = Convert.ToInt32( * widthRatio); if ( > desiredWidth) { = desiredWidth; = Convert.ToInt32( * heightRatio); } return imageSize; } /// <summary> /// Get all relevant information about the image encoding codec /// </summary> /// <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)); } /// <summary> /// Save pictures with high quality /// </summary> /// <param name="image"></param> /// <param name="savePath"></param> /// <param name="ici"></param> 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.