Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Drawing. Imaging;
Namespace CutPicture
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
// Load the image
Private void loadSrcBtn_Click (object sender, EventArgs e)
{
OpenFileDialog srcImageName = new OpenFileDialog ();
If (srcImageName. ShowDialog () = DialogResult. OK)
{
Bitmap bmp = new Bitmap (Image. FromFile (srcImageName. FileName ));
SrcImage. Image = bmp;
}
}
// Main image capturing operation area
Private void cutImage (Point pos, int cutWidth, int cutHeight)
{
// Initialize a bitmap object to store the captured image.
Bitmap bmp dest = new Bitmap (cutWidth, cutHeight, PixelFormat. Format32bppRgb );
// This rectangle defines the position and size of the Left vertex of the image area to be captured on the captured image.
Rectangle rectSource = new Rectangle (pos. X, pos. Y, cutWidth, cutHeight );
// This rectangle defines the position and size of the captured image area to the initialized bitmap.
// My definition indicates that I will plot the intercepted area from the left vertex of the bitmap and draw the original size of the intercepted area.
Rectangle rectDest = new Rectangle (0, 0, cutWidth, cutHeight );
// The first parameter is to load the image object you want to intercept, the second and third parameters, and the relevant attributes defined above during image capturing and rendering, the fourth attribute defines the measurement unit used by the attribute value.
G. DrawImage (SrcImage. Image, rectDest, rectSource, GraphicsUnit. Pixel );
// This is to display the captured image on the GUI
CutedImage. Image = (Image) bmp dest;
G. Dispose ();
}
// Save the captured image
Private void saveCutImage_Click (object sender, EventArgs e)
{
SaveFileDialog saveImageName = new SaveFileDialog ();
If (saveImageName. ShowDialog () = DialogResult. OK)
{
CutedImage. Image. Save (saveImageName. FileName );
}
}
// Show the captured image
Private void showCutImageBtn_Click (object sender, EventArgs e)
{
Point pos = new Point (0, 0 );
If (! (PosX. Text = "" | posY. Text = ""))
{
Pos. X = int. Parse (posX. Text );
Pos. Y = int. Parse (posY. Text );
}
Int cutWidth, cutHeight;
CutWidth = 20;
CutHeight = 20;
If (! (CutImageWidth. Text = "" | cutImageHeight. Text = ""))
{
CutWidth = int. Parse (cutImageWidth. Text );
CutHeight = int. Parse (cutImageHeight. Text );
}
This. cutImage (pos, cutWidth, cutHeight );
}
}
}
Below is the demo