SoFunction
Updated on 2025-03-07

How to draw C# square pictures

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);
      ();
    }

    /// &lt;summary&gt;
    /// Get all relevant information about the image encoding codec    /// &lt;/summary&gt;
    /// <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;
    }

    /// &lt;summary&gt;
    /// Calculate the new size    /// &lt;/summary&gt;
    /// <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>    /// &lt;returns&gt;&lt;/returns&gt;
    private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
    {
      //This time it was modified on 2012-02-05============================================================================================================      if (maxWidth &lt;= 0)
        maxWidth = width;
      if (maxHeight &lt;= 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 &gt; MAX_WIDTH || originalHeight &gt; MAX_HEIGHT)
      {
        decimal factor;
        // determine the largest factor 
        if (originalWidth / originalHeight &gt; 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);
    }

    /// &lt;summary&gt;
    /// Get the picture format    /// &lt;/summary&gt;
    /// <param name="name">File name</param>    /// &lt;returns&gt;&lt;/returns&gt;
    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 ;
      }
    }
   

    /// &lt;summary&gt;
    /// Make small squares    /// &lt;/summary&gt;
    /// <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 &gt; 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 &lt; 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
      {
        ();
        ();
      }
    }

    /// &lt;summary&gt;
    /// Make small squares    /// &lt;/summary&gt;
    /// <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.