How to select region on PictureBox.Image with mouse in C #

I just wanted to put the highlight on my picturebox.image file, but it only got worse than some kind of slightly annoying situation. I thought that on another picture box above the main picture box, but it seemed to me that this lazy work. I need to know if there is a way to create a selection area (which will be a translucent blue area) in the picture. The image that will be drawn with the mouse, and should not change the im image that works.

Example:

// Start Rectangle // private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { // Determine the initial rectangle coordinates... RectStartPoint = e.Location; Invalidate(); } // Draw Rectangle // private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; Point tempEndPoint = e.Location; Rect = new Rectangle( Math.Min(RectStartPoint.X, tempEndPoint.X), Math.Min(RectStartPoint.Y, tempEndPoint.Y), Math.Abs(RectStartPoint.X - tempEndPoint.X), Math.Abs(RectStartPoint.Y - tempEndPoint.Y)); Invalidate(Rect); } // Draw Area // private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { // Draw the rectangle... if (pictureBox1.Image != null) { Brush brush = new SolidBrush(Color.FromArgb(128, 72, 145, 220)); e.Graphics.FillRectangle(brush, Rect); } } 
+8
c # winforms system.drawing
source share
3 answers

I used your code, you were almost there. You needed to make Invalidate pictureBox1 instead of a rectangle. I also added a check for Rect so that it does not draw when it is not initialized or has no size.

Another important change: I created the Rectangle only once, and I adjusted its location and size. Less trash to clean!

EDIT

I added a right-click handler for the rectangle.

 private Point RectStartPoint; private Rectangle Rect = new Rectangle(); private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220)); // Start Rectangle // private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { // Determine the initial rectangle coordinates... RectStartPoint = e.Location; Invalidate(); } // Draw Rectangle // private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; Point tempEndPoint = e.Location; Rect.Location = new Point( Math.Min(RectStartPoint.X, tempEndPoint.X), Math.Min(RectStartPoint.Y, tempEndPoint.Y)); Rect.Size = new Size( Math.Abs(RectStartPoint.X - tempEndPoint.X), Math.Abs(RectStartPoint.Y - tempEndPoint.Y)); pictureBox1.Invalidate(); } // Draw Area // private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { // Draw the rectangle... if (pictureBox1.Image != null) { if (Rect != null && Rect.Width > 0 && Rect.Height > 0) { e.Graphics.FillRectangle(selectionBrush, Rect); } } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (Rect.Contains(e.Location)) { Debug.WriteLine("Right click"); } } } 
+31
source share
  private int xUp, yUp, xDown,yDown; private Rectangle rectCropArea; private void SrcPicBox_MouseUp(object sender, MouseEventArgs e) { //pictureBox1.Image.Clone(); xUp = eX; yUp = eY; Rectangle rec = new Rectangle(xDown,yDown,Math.Abs(xUp xDown),Math.Abs(yUp-yDown)); using (Pen pen = new Pen(Color.YellowGreen, 3)) { SrcPicBox.CreateGraphics().DrawRectangle(pen, rec); } rectCropArea = rec; } private void SrcPicBox_MouseDown(object sender, MouseEventArgs e) { SrcPicBox.Invalidate(); xDown = eX; yDown = eY; } private void btn_upload_Click(object sender, EventArgs e) { OpenFileDialog opf = new OpenFileDialog(); // PictureBox SrcPicBox = new PictureBox(); opf.Filter = "ALL images(*.*)|*.*"; if (opf.ShowDialog() == DialogResult.OK) { string name = opf.SafeFileName; string filepath = opf.FileName; File.Copy(filepath, name, true); SrcPicBox.Image = Image.FromFile(opf.FileName); } private void btn_crop_Click(object sender, EventArgs e) { pictureBox3.Refresh(); //Prepare a new Bitmap on which the cropped image will be drawn Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image, SrcPicBox.Width, SrcPicBox.Height); Graphics g = pictureBox3.CreateGraphics(); //Draw the image on the Graphics object with the new dimesions g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox3.Width, pictureBox3.Height), rectCropArea, GraphicsUnit.Pixel); sourceBitmap.Dispose(); } 
0
source share

Thanks for your example, I am using the code provided by Erno, with some modifications, such as DrawRect instead of FillRect to draw the selected area.

However, I have some problems saving the selected area in JPG format. I use this code, but it seems that the Rect links are not suitable for this, I keep the neighboring area and not the exact location.

Does anyone have an idea on how to keep the exact location on a PictureBox image?

  private void ButtonExport_Click(object sender, EventArgs e) { //exporter la sélection Bitmap bmp = new Bitmap(Rect.Size.Width,Rect.Size.Height); using (Graphics gr = Graphics.FromImage(bmp)) { gr.DrawImage(pictureBox1.Image, new Rectangle(0, 0, Rect.Size.Width, Rect.Size.Height), Rect, GraphicsUnit.Pixel); } bmp.Save(Directory.GetCurrentDirectory() + "\\Archives\\" + numFiche.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } 
0
source share

All Articles