SoFunction
Updated on 2025-03-07

C# implements mouse image cropping function

This article shares the specific code for C# to implement mouse cropping images for your reference. The specific content is as follows

Image cropping of C# is easy to operate, here is an implementation example.

The key is to handle mouse events and some updates

Implement mouse movement code. Be careful not to repaint all updates, only select the rectangle part to repaint

private void Form1_MouseMove(object sender, MouseEventArgs e)
 {

  if (Track_move)
  endpoint = new Point(, );
  else
  {
  return;
  }
  rect1 = new Rectangle(, ,  - ,  - );

  Rectangle tempr = new Rectangle(, ,  + 2,  + 2);
  (tempr);
 }

Select the ending processing code.

private void Form1_MouseUp(object sender, MouseEventArgs e)
 {
  if ( ==  && Track_move==true )
  {
  Track_move = false;
  endpoint = new Point(, );
  rect1 = new Rectangle(, ,  - ,  - );
  Rectangle rectorg = new Rectangle(, , , );
  if ( <= 0)
   return;
  if ( <= 0)
   return;
  if ((rect1))
  {
   Rectangle rectadj = new Rectangle( - ,  - , , );
   Bitmap cropimge = (rectadj, .Format24bppRgb);
    = cropimge;
  }
  else
  {
    = null;
  }
  ();
 }
}

The entire code of the program

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

namespace imageForms
{
 static class Program
 {
 /// &lt;summary&gt;
 /// The main entry point of the application. /// &lt;/summary&gt;
 [STAThread]
 static void Main()
 {
  ();
  (false);
  (new Form1());
 }
 }
 public partial class Form1 : Form
 {
 private  pictureBox2;
 private  label1;
 public Form1()
 {
  InitializeComponent();
 }

 private void pictureBox1_Click(object sender, EventArgs e)
 {

 }

 private void Form1_Load(object sender, EventArgs e)
 {
  showimg();

 }
 Bitmap image1;
 private void showimg()
 {
  int wd = 400;
  int hg = 200;
  int len = wd * hg * 3;
  byte[] pdata = new byte[len];
  for (int i = 0; i &lt; len; i++)
  {
  if (i &gt; 3 * wd * (hg / 2))
  {
   pdata[i] = 255;
  }
  else
  {
   pdata[i] = 0;
  }
  }

  try
  {
  image1 = new Bitmap(wd, hg, .Format24bppRgb);
  for (int y = 0; y &lt; hg; y++)
  {
   for (int x = 0; x &lt; wd; x++)
   {
   Color crr = (pdata[3 * wd * y + x], pdata[3 * wd * y + x], pdata[3 * wd * y + x]);
   (x, y, crr);
   }
  }
  // Set the PictureBox to display the image.
  //  = image1;

  }
  catch (ArgumentException)
  {
  ("There was an error check data.");
  }
 }
 Point stpoint,endpoint; 
 Rectangle rect1; 
 Point borg = new Point(20, 20);
 protected override void OnPaint(PaintEventArgs e)
 {
  
  (e);
  (image1, borg);
  if (rect1 != null )
  {
   (new Pen(, 1), rect1);
  }
  
 }

 private void Form1_MouseDown(object sender, MouseEventArgs e)
 {
  if ( == )
  {
  stpoint = new Point(, );
  Track_move = true;
  return;
  }
  Track_move = false;
 }

 private void Form1_MouseUp(object sender, MouseEventArgs e)
 {
  if ( ==  &amp;&amp; Track_move==true )
  {
  Track_move = false;
  endpoint = new Point(, );
  rect1 = new Rectangle(, ,  - ,  - );
  Rectangle rectorg = new Rectangle(, , , );
  if ( &lt;= 0)
   return;
  if ( &lt;= 0)
   return;
  if ((rect1))
  {
   Rectangle rectadj = new Rectangle( - ,  - , , );
   Bitmap cropimge = (rectadj, .Format24bppRgb);
    = cropimge;
  }
  else
  {
    = null;
  }
  ();
  }
 }
 bool Track_move=false ;
 private void Form1_MouseMove(object sender, MouseEventArgs e)
 {

  if (Track_move)
  endpoint = new Point(, );
  else
  {
  return;
  }
  rect1 = new Rectangle(, ,  - ,  - );

  Rectangle tempr = new Rectangle(, ,  + 2,  + 2);
  (tempr);
 }
 private  components = null;

 private void InitializeComponent()
 {
  this.pictureBox2 = new ();
  this.label1 = new ();
  (()(this.pictureBox2)).BeginInit();
  ();
  // 
  // pictureBox2
  // 
  this. = new (605, 103);
  this. = "pictureBox2";
  this. = new (227, 173);
  this. = 1;
  this. = false;
  // 
  // label1
  // 
  this. = true;
  this. = new (602, 58);
  this. = "label1";
  this. = new (127, 15);
  this. = 2;
  this. = "Select cropping with the left mouse button";
  // 
  // Form1
  // 
   = new (8F, 15F);
   = ;
   = new (844, 558);
  (this.label1);
  (this.pictureBox2);
   = "Form1";
   = "Form1";
   += new (this.Form1_Load);
   += new (this.Form1_MouseUp);
   += new (this.Form1_MouseDown);
   += new (this.Form1_MouseMove);
  (()(this.pictureBox2)).EndInit();
  (false);
  ();

 }
 protected override void Dispose(bool disposing)
 {
  if (disposing &amp;&amp; (components != null))
  {
  ();
  }
  (disposing);
 }

 }
}

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.