Method of Image Cutting Based on C

  • 2021-12-19 06:32:51
  • OfStack

In this paper, an example is given to describe the method of realizing picture cutting by C #. Share it for your reference, as follows:

Picture cutting is to cut a large picture into several small pictures according to user requirements. The GDI + class library is provided in the dotnet environment, which provides a convenient interface for image 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"> Height of image after cutting </param>
  /// <param name="savePath"> Save path of cut image file </param>
  /// <param name="fileExt"> Image file extension after cutting </param>
  public static void Cut(string url, int width, int height,string savePath,string fileExt,string logofile)
  {
   Bitmap bitmap = new Bitmap(url);
   Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
   Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / width);
   for (decimal i = 0; i < MaxRow; i++)
   {
    for (decimal j = 0; j < MaxColumn; j++)
    {
     string filename = i.ToString() + "," + j.ToString() + "." + fileExt;
     Bitmap bmp = new Bitmap(width, height);
     for (int offsetX = 0; offsetX < width; offsetX++)
     {
      for (int offsetY = 0; offsetY < height; offsetY++)
      {
       if (((j * width + offsetX) < bitmap.Width) && ((i * height + offsetY) < bitmap.Height))
       {
        bmp.SetPixel(offsetX, offsetY, bitmap.GetPixel((int)(j * width + offsetX), (int)(i * height + offsetY)));
       }
      }
     }
     Graphics g = Graphics.FromImage(bmp);
     g.DrawString(" Script House ", new Font(" Blackbody ", 20), new SolidBrush(Color.FromArgb(70, Color.WhiteSmoke)), 60, height/2);// Watermarking 
     ImageFormat format = ImageFormat.Png;
     switch (fileExt.ToLower())
     {
      case "png":
       format = ImageFormat.Png;
       break;
      case "bmp":
       format = ImageFormat.Bmp;
       break;
      case "gif":
       format = ImageFormat.Gif;
       break;
     }
     bmp.Save(savePath+"//" + filename,format);
    }
   }
  }
}

Programmers only need to call Cut function to apply it.

For more readers interested in C # related content, please check the topics on this site: "Summary of C # Picture Operation Skills", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

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


Related articles: