How to change drawn rectangle/s size using a trackBar1 scroll event?

It’s a bit long, but it’s all connected.

At the top of form1:

List<DrawingRectangle> DrawingRects = new List<DrawingRectangle>();

The mouse events to draw the rectangles:

MouseDown:

private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;

            x = 0;
            y = 0;

            if (pictureBox2.Image != null && selectedPath != null)
            {
                if ((x >= 0 && x <= pictureBox2.Image.Size.Width) && (y >= 0 && y <= pictureBox2.Image.Size.Height))
                {
                    DrawingRects.Add(new DrawingRectangle()
                    {
                        Location = e.Location,
                        Size = Size.Empty,
                        StartPosition = e.Location,
                        Owner = (Control)sender,
                        DrawingcColor = SelectedColor
                    });
                }
            }
        }

MouseMove

private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            int X = e.X;
            int Y = e.Y;

            if (e.Button != MouseButtons.Left) return;


            if ((X >= 0 && X <= pictureBox2.Width) && (Y >= 0 && Y <= pictureBox2.Height))
            {
                if (pictureBox2.Image != null && selectedPath != null && DrawingRects.Count > 0)
                {
                    if ((x >= 0 && x <= pictureBox2.Image.Size.Width) && (y >= 0 && y <= pictureBox2.Image.Size.Height))
                    {
                        x = e.X;
                        y = e.Y;

                        var dr = DrawingRects[DrawingRects.Count - 1];
                        if (e.Y < dr.StartPosition.Y) { dr.Location = new Point(dr.Rect.Location.X, e.Y); }
                        if (e.X < dr.StartPosition.X) { dr.Location = new Point(e.X, dr.Rect.Location.Y); }

                        dr.Size = new Size(Math.Abs(dr.StartPosition.X - e.X), Math.Abs(dr.StartPosition.Y - e.Y));
                        pictureBox2.Invalidate();
                    }
                }
            }
        }

MouseUp

int count = 0;
        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)
                        {
                            count++;
                            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.WriteLine("Total number of rectangles: " + count + Environment.NewLine);
                                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;
                }
            }
        }

Then the 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);
            }
        }

The DrawShapes method:

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);
                    };
                }
            }
        }

the scroll event of the trackBar1:

private void trackBar1_Scroll(object sender, EventArgs e)
        {
            DrawingRectangle dr = new DrawingRectangle();
            dr.PenSize = trackBar1.Value;
            pictureBox2.Invalidate();
        }

and last the DrawingRectangle class:

public class DrawingRectangle
        {
            public Rectangle Rect => new Rectangle(Location, Size);
            public Size Size { get; set; }
            public Point Location { get; set; }
            public Control Owner { get; set; }
            public Point StartPosition { get; set; }
            public Color DrawingcColor { get; set; } = Color.LightGreen;
            public float PenSize { get; set; } = 3f;
        }

I want in the trackBar1 scroll event when changing the trackBar1 event that it will change the PenSize to the trackBar1 value and then to update the already drawn rectangle/s so I did:

pictureBox2.Invalidate();

in the trackBar1_Scroll event but it’s not affecting the drawn rectangle/s.

I tried the code in the trackBar1_Scroll event but it’s not changing in any way the drawn rectangle/s size.

>Solution :

To change the pen size of the already drawn rectangles when the trackBar1 value is changed, you need to update the PenSize property of each DrawingRectangle object in the DrawingRects list and then invalidate the pictureBox2 control to trigger the pictureBox2_Paint event to redraw the rectangles with the new pen size.

private void trackBar1_Scroll(object sender, EventArgs e)
{
    // Set the pen size of new rectangles to the trackBar1 value
    DrawingRectangle.PenSize = trackBar1.Value;

    // Update the pen size of existing rectangles
    foreach (var dr in DrawingRects)
    {
        dr.PenSize = DrawingRectangle.PenSize;
    }

    // Invalidate the pictureBox2 control to redraw the rectangles
    pictureBox2.Invalidate();
}

Note that the DrawingRectangle class is not static and doesn’t have a static PenSize property, so you need to create an instance of the class to set its PenSize property. If you want to set the PenSize property for new rectangles without creating an instance of the class, you can make the PenSize property static by adding the static keyword to its definition:

public static float PenSize { get; set; } = 3f;

Then you can set the PenSize property like this:

DrawingRectangle.PenSize = trackBar1.Value;

Hope this helps

Leave a Reply