This article has shared with you the specific code for drawing square pictures in C# for your reference. The specific content is as follows
using System; using ; using ; using .Drawing2D; using ; using ; using ; using ; namespace treads { /// <summary> /// Make small squares /// </summary> class Class3 { private string srcFileName = @"x";//Get the path to the image private string srcFileName1 = @"x";//Keep the new path to the picture /// <summary> /// Save the picture /// </summary> /// <param name="image">Image object</param> /// <param name="savePath">SavePath</param> /// <param name="ici">Designed format codec parameters</param> private static void SaveImage(Image image, string savePath, ImageCodecInfo ici) { //Set the EncoderParameters object of the original image object EncoderParameters parameters = new EncoderParameters(1); [0] = new EncoderParameter(, ((long)100)); (savePath, ici, parameters); (); } /// <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> private static ImageCodecInfo GetCodecInfo(string mimeType) { ImageCodecInfo[] CodecInfo = (); foreach (ImageCodecInfo ici in CodecInfo) { if ( == mimeType) return ici; } return null; } /// <summary> /// Calculate the new size /// </summary> /// <param name="width">original width</param> /// <param name="height">original height</param> /// <param name="maxWidth">Maximum new width</param> /// <param name="maxHeight">Maximum new height</param> /// <returns></returns> private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight) { //This time it was modified on 2012-02-05============================================================================================================ if (maxWidth <= 0) maxWidth = width; if (maxHeight <= 0) maxHeight = height; //The above 2012-02-05 has been modified ============================================================================================================= decimal MAX_WIDTH = (decimal)maxWidth; decimal MAX_HEIGHT = (decimal)maxHeight; decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT; int newWidth, newHeight; decimal originalWidth = (decimal)width; decimal originalHeight = (decimal)height; if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT) { decimal factor; // determine the largest factor if (originalWidth / originalHeight > ASPECT_RATIO) { factor = originalWidth / MAX_WIDTH; newWidth = Convert.ToInt32(originalWidth / factor); newHeight = Convert.ToInt32(originalHeight / factor); } else { factor = originalHeight / MAX_HEIGHT; newWidth = Convert.ToInt32(originalWidth / factor); newHeight = Convert.ToInt32(originalHeight / factor); } } else { newWidth = width; newHeight = height; } return new Size(newWidth, newHeight); } /// <summary> /// Get the picture format /// </summary> /// <param name="name">File name</param> /// <returns></returns> public static ImageFormat GetFormat(string name) { string ext = ((".") + 1); switch (()) { case "jpg": case "jpeg": return ; case "bmp": return ; case "png": return ; case "gif": return ; default: return ; } } /// <summary> /// Make small squares /// </summary> /// <param name="image">Image object</param> /// <param name="newFileName">New Address</param> /// <param name="newSize">Length or Width</param> public static void MakeSquareImage(Image image, string newFileName, int newSize) { int i = 0; int width = ; int height = ; if (width > height) i = height; else i = width; Bitmap b = new Bitmap(newSize, newSize); try { Graphics g = (b); //Set high-quality interpolation method = ; //Set high quality and low speed to show smoothness = ; = ; //Clear the entire drawing surface and fill it with transparent background color (); if (width < height) (image, new Rectangle(0, 0, newSize, newSize), new Rectangle(0, (height - width) / 2, width, width), ); else (image, new Rectangle(0, 0, newSize, newSize), new Rectangle((width - height) / 2, 0, height, height), ); SaveImage(b, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower())); } finally { (); (); } } /// <summary> /// Make small squares /// </summary> /// <param name="fileName">Image file name</param> /// <param name="newFileName">New Address</param> /// <param name="newSize">Length or Width</param> public static void MakeSquareImage(string fileName,string newFileName, int newSize) { MakeSquareImage((fileName), newFileName, newSize); } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.