0

I've been drawn multiple rectangles on the picturebox image.I want to resize all that rectangles using mouse events. Can anyone help me out regarding this?

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 to my application.I drawn rectangles in 2*3 manner.and also draw a big rectangle above it.In short my picturebox containing many rectangles and i want to add resizing options for all the rectangles in c#

  • Now we know a bit about what you're trying to achieve, but what is your question? Have you tried anything? – C.Evenhuis Apr 06 '18 at 13:08
  • See [this example of a selectable Line class](https://stackoverflow.com/questions/32919918/how-to-draw-line-and-select-it-in-panel/32920894#32920894) to get you going on the basics.. – TaW Apr 06 '18 at 13:09
  • Sorry to say it, but you're really far away from what you want to accomplish. See the answer @TaW posted: one of key notes is *Never cache or store a Grahics object.*. It's the beginning. A mix of links: [Simple Vector Shapes](https://www.codeproject.com/Articles/19195/Simple-Vector-Shapes), [Shape Control for .NET](https://www.codeproject.com/Articles/10558/Shape-Control-for-NET), [Controlling Drawn Shapes in C#](https://www.codeproject.com/Articles/146648/Controlling-Drawn-Shapes-in-C). WPF? [Shape Class](https://msdn.microsoft.com/en-us/library/system.windows.shapes.shape.aspx) – Jimi Apr 06 '18 at 13:24

0 Answers0