0

In some images it's cropping the right parts but in other images it's displaying on the left pictureBox1 the wrong cropped part. in both pictureBoxes 1 and 2 i'm using normal SizeMode.

This screenshot show correct cropped image. On the right the rectangle i draw and this cropped part of the image in the rectangle show on the left pictureBox1 : The image size is 1024 x 640

working cropped image

in this screenshot i loaded to pictureBox2 image size 634 x 795 again drawing a rectangle with the mouse on the pictureBox2 but in pictureBox1 it's showing something else : it looks like in this image the cropped part in pictureBox1 on the left is bigger or zommed in.

the cropped image part in pictureBox1 is not the same size in the pictureBox2 drawn rectangle

This method DRawShapes draw the rectangle on the pictureBox2 :

private void DrawShapes(Graphics g)
        {
            if (DrawingRects.Count == 0) return;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            foreach (var dr in DrawingRects)
            {
                if (dr.Rect.Width > 0 && dr.Rect.Height > 0)
                {
                    using (Pen pen = new Pen(dr.DrawingcColor, dr.PenSize))
                    {
                        g.DrawRectangle(pen, dr.Rect);
                    };
                }
            }
        }

Using this method in pictureBox2 paint event :

private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            if (drawBorder)
            {
                ControlPaint.DrawBorder(e.Graphics, pictureBox2.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
            }

            if (pictureBox2.Image != null && selectedPath != null && DrawingRects.Count > 0)
            {
                DrawShapes(e.Graphics);
            }
        }

This method should make the crop and display it on the pictureBox1 :

public Bitmap cropAtRect(Bitmap b, Rectangle r)
        {
            Bitmap nb = new Bitmap(r.Width, r.Height);
            using (Graphics g = Graphics.FromImage(nb))
            {
                g.DrawImage(b, -r.X, -r.Y);
                return nb;
            }
        }

Using this method :

private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            if (DrawingRects.Count > 0 && pictureBox2.Image != null && selectedPath != null)
            {
                if ((x >= 0 && x <= pictureBox2.Image.Size.Width) && (y >= 0 && y <= pictureBox2.Image.Size.Height))
                {
                    var dr = DrawingRects.Last();
                    if (dr.Rect.Width > 0 && dr.Rect.Height > 0)
                    {
                        rectImage = cropAtRect((Bitmap)pictureBox2.Image, dr.Rect);
                        if (saveRectangles)
                        {


                            rectangleName = GetNextName(Path.Combine(selectedPath, "Rectangle"), ".bmp"); 
                            FileList.Add($"{dr.Location}, {dr.Size}", rectangleName);
                            string json = JsonConvert.SerializeObject(
        FileList,
        Formatting.Indented
    );
                            using (StreamWriter sw = new StreamWriter(Path.Combine(selectedPath, "rectangles.txt"), false))
                            {
                                sw.Write(json);
                                sw.Close();
                            }

                            rectImage.Save(rectangleName);
                            saveRectanglesCounter++;
                        }
                        pixelsCounter = rect.Width * rect.Height;

                        pictureBox1.Invalidate();
                        
                        listBox1.DataSource = FileList.Keys.ToList();
                        listBox1.SelectedIndex = listBox1.Items.Count - 1;

                        pictureBox2.Focus();
                        Graphics g = Graphics.FromImage(this.pictureBox1.Image);
                        g.Clear(this.pictureBox1.BackColor);
                    }
                }
                else
                {
                    if (clearRectangles)
                    {
                        DrawingRects.Clear();
                        pictureBox2.Invalidate();
                    }

                    x = 0;
                    y = 0;
                    
                }
            }
        }

This line :

rectImage is a BitMap type :

rectImage = cropAtRect((Bitmap)pictureBox2.Image, dr.Rect);

and then in pictureBox1 paint event :

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (drawBorder)
            {
                ControlPaint.DrawBorder(e.Graphics, pictureBox1.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
            }
            if (rectImage != null && DrawingRects.Count > 0)
            {
                var dr = DrawingRects.Last();
                e.Graphics.DrawImage(rectImage, dr.Rect);

                if (clearRectangles)
                {
                    DrawingRects.Clear();
                    pictureBox2.Invalidate();
                }
            }
        }

The results are that when loading some image/s to the pictureBox2 and crop part of the image it's working fine but other images with different size it's not cropping it right.

  • 1
    **IF** the SizeMode is actually set to Normal (though, in this case, part of the images is not visible in the PictureBoxes), see the note here: [Image is not drawn at the correct spot](https://stackoverflow.com/a/51456467/7444103) or here: [Position and size of combined images inside a new Bitmap](https://stackoverflow.com/a/69021795/7444103) – Jimi Oct 14 '22 at 17:52
  • 1
    If the SizeMode is (or can be) different, combine with Reza Aghaei's answer here: [Translate Rectangle Position in Zoom Mode Picturebox](https://stackoverflow.com/q/53800328/7444103): it can be used to avoid other calculations, since the PictureBox Control already does this internally (and, for unspecified reasons, it doesn't *expose the numbers*) – Jimi Oct 14 '22 at 18:00

0 Answers0