SoFunction
Updated on 2025-03-06

C# method to realize image cutting

This article describes the method of C# to implement image cutting. Share it for your reference, as follows:

Image cutting is to cut a large picture into multiple small pictures according to user requirements. The system provides a GDI+ class library in the dotnet environment, providing a convenient interface for image operation processing.

The following is the image cutting applet:

public class ImageManager
{
  /// <summary>
  /// Image cutting  /// </summary>
  /// <param name="url">Image file name</param>  /// <param name="width">Image width after cutting</param>  /// <param name="height">Image height after cutting</param>  /// <param name="savePath">SavePath after cutting</param>  /// <param name="fileExt">Sliced ​​image file extension</param>  public static void Cut(string url, int width, int height,string savePath,string fileExt,string logofile)
  {
   Bitmap bitmap = new Bitmap(url);
   Decimal MaxRow = ((Decimal) / height);
   Decimal MaxColumn = ((decimal) / width);
   for (decimal i = 0; i &lt; MaxRow; i++)
   {
    for (decimal j = 0; j &lt; MaxColumn; j++)
    {
     string filename = () + "," + () + "." + fileExt;
     Bitmap bmp = new Bitmap(width, height);
     for (int offsetX = 0; offsetX &lt; width; offsetX++)
     {
      for (int offsetY = 0; offsetY &lt; height; offsetY++)
      {
       if (((j * width + offsetX) &lt; ) &amp;&amp; ((i * height + offsetY) &lt; ))
       {
        (offsetX, offsetY, ((int)(j * width + offsetX), (int)(i * height + offsetY)));
       }
      }
     }
     Graphics g = (bmp);
     ("I", new Font("Bold", 20), new SolidBrush((70, )), 60, height/2);//Add a watermark     ImageFormat format = ;
     switch (())
     {
      case "png":
       format = ;
       break;
      case "bmp":
       format = ;
       break;
      case "gif":
       format = ;
       break;
     }
     (savePath+"//" + filename,format);
    }
   }
  }
}

Programmers only need to call the Cut function to apply.

For more information about C# related content, please check out the topic of this site:Summary of C# picture operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.