Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I draw multiple rectangles on picturebox image . i want to resize that all rectangles using mouse in c# . Can anyone help me .. Thank You

What I have tried:

C#
public List<rectangle> listRec = new List<rectangle>();
Graphics g;
//private Graphics g;
Point startPos;
Point currentPos;
bool drawing;
Rectangle r1;
Rectangle rect = new Rectangle();

private Rectangle getRectangle()
{
    r1 = new Rectangle(
        Math.Min(startPos.X, currentPos.X),
        Math.Min(startPos.Y, currentPos.Y),
        Math.Abs(startPos.X - currentPos.X),
        Math.Abs(startPos.Y - currentPos.Y));
    return r1;
}

private void button1_Click(object sender, EventArgs e)
{
    String data;

    Font font = new Font("Arial", 14);
    arg1 = Convert.ToInt32(textBox1.Text);
    arg2 = Convert.ToInt32(textBox2.Text);
    Rectangle rect = new Rectangle();
    rect.Size = new Size(40, 65);
    for (int x = 0; x < arg1; x++)
    {
        // rect.X = x * rect.Width;
        rect.X = x * (rect.Width + 30) + 73;
        for (int y = 0; y < arg2; y++)
        {
            rect.Y = y * (rect.Height + 35) + 38;
            listRec.Add(rect);
            data = rect.ToString();
            TextWriter txt = new StreamWriter("E:\\B1Pockets.txt", true);
            txt.WriteLine(data);
            txt.Close();
            // MessageBox.Show(rect.ToString());
        }
    }

    foreach (Rectangle rec in listRec)
    {
        g = pictureBox1.CreateGraphics();
        Pen p = new Pen(Color.Red, 3);
        g.DrawRectangle(p, rec);
        g.DrawString("p1", font, new SolidBrush(Color.Yellow), (rect.Width + 30), 35);
        g.DrawString("p2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, 35);
        g.DrawString("p3", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, 35);
        g.DrawString("p4", font, new SolidBrush(Color.Yellow), (rect.Width + 30), (rect.Height + 30) + 40);
        g.DrawString("p5", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, (rect.Height + 30) + 40);
        g.DrawString("p6", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, (rect.Height + 30) + 40);
    }
}


I've tried this code .
Posted
Updated 6-Apr-18 4:01am
v2
Comments
BillWoodruff 6-Apr-18 21:45pm    
Yes, you can do this, but you need to clarify what changes size ... the PictureBox ? And how is the whatever control's size changed at run-time: by entering numbers in TextBoxes ? by direct action with the mouse.

1 solution

You need to capture the mouse button down, mouse move and mouse button up notifications. At each move you store the x and y co-ordinates, then when the mouse is released you can redraw the rectangle using the new co-ordinates.

See Mouse Input Notifications (Windows)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900