C # capturing specific areas in an image

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.