Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey ,
I am making drawing on bitmap images ,In drawings I've included only rectangles . I want to move all my rectangles together as a group . Can anyone help me regarding this ?

What I have tried:

C#
String data = "";
            Font font = new Font("Arial", 14);
         
            Rectangle rect = new Rectangle();
            rect.Size = new Size(280, 190);
            for (int x = 0; x < 1; x++)
            {

                rect.X = x * (rect.Width + 45) + 380;
                for (int y = 0; y < 1; y++)
                {
                    rect.Y = y * (rect.Height + 35) + 25;
                    listRec.Add(rect);
                    data = rect.ToString();
                    TextWriter txt = new StreamWriter("E:\\Blisters.txt", true);
                    txt.WriteLine(data);
                    txt.Close();
                }
            }

            foreach (Rectangle rec in listRec)
            {
                g = pictureBox1.CreateGraphics();
                Pen p = new Pen(Color.Red, 3);
                g.DrawRectangle(p, rec);
               // g.DrawString("B1", font, new SolidBrush(Color.Yellow), 55, 25);
                g.DrawString("B2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, 25);

            }
            //    String data;
            //Font font = new Font("Arial", 14);
            //Rectangle rect = new Rectangle();
            rect.Size = new Size(40, 65);
            for (int x = 0; x < 3; x++)
            {
                // rect.X = x * rect.Width;
                rect.X = x * (rect.Width + 30) + 463;
                for (int y = 0; y < 2; y++)
                {
                    rect.Y = y * (rect.Height + 35) + 38;
                    listRec.Add(rect);
                    data = rect.ToString();
                    TextWriter txt = new StreamWriter("E:\\B2Pockets.txt", true);
                    txt.WriteLine(data);
                    txt.Close();
                }
            }


            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 + 420), 35);
                g.DrawString("p2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 450, 35);
                g.DrawString("p3", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 520, 35);
                g.DrawString("p4", font, new SolidBrush(Color.Yellow), (rect.Width + 420), (rect.Height + 30) + 40);
                g.DrawString("p5", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 450, (rect.Height + 30) + 40);
                g.DrawString("p6", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 520, (rect.Height + 30) + 40);
            }

I've tried above code.Rectangles drawn successfully. But they can't be move.
Posted
Updated 1-Apr-18 23:43pm
v5

1 solution

This CP article might be helpful:

Controlling Drawn Shapes in C#[^]
 
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