Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All ,
I want to draw multiple rectangles on an image based on image size , for example if i have image Width=733 , height=501 , and i want to draw 6*8 rectangles i.e 6 rows and 8 column rectangles . how can i draw .

Currently i'm able to draw multiple rectangles but not getting the correct logic , what will be the better way to do that .

What I have tried:

public Form1()
{
InitializeComponent();
int j = pictureBox1.Location.X;
int k = pictureBox1.Location.Y;

int imagewidth = pictureBox1.Image.Size.Width;
int imageheight = pictureBox1.Image.Size.Height;

System.Drawing.Image img = System.Drawing.Image.FromFile(@"E:\Caledonia.jpg");
MessageBox.Show("Width: " + img.Width + ", Height: " + img.Height);

var size = pictureBox1.ClientSize;
// getting multiple rectangles
for (k = 10; k <= 1500; k++)
{
//for (j = 13; j <= img.Width; j++)
//{
// j = j - 1;
k = k - 1;
rect = new UserRect(new Rectangle(j, k, 200, 150));
listRec.Add(rect.rect);
rect.SetPictureBox(this.pictureBox1);
//i = i + 20;
//j = j + 12;
j = j + 100;
k = k + 100;
//}


}
}
Posted
Updated 8-May-17 22:17pm
Comments
Ralf Meier 9-May-17 1:49am    
Do you want that those rectangles become part of the Image ? Or do you want to have a kind of Layer over the Image (without changing it) ?
Mallesh Shantagiri 9-May-17 2:09am    
i just want those on top of image , those should be in order.
Ralf Meier 9-May-17 2:47am    
That doesn't answer my question. I repeat it :
Do you want that those rectangles become part of your Image - do you want to paint directly onto the Image ?
Mallesh Shantagiri 9-May-17 3:08am    
Yes , i want those rectangles will be the part of image . On top of the image size .
Mallesh Shantagiri 9-May-17 3:16am    
i have drawn one column of rectangles , just by keeping image x axis as constant and Y axis as increment with total height/4 , but now i want to do that for the whole image.

1 solution

my Suggestion is to create a customized PictureBox with the Behaviour you want to have :

C#
using System;
using System.Windows.Forms;
using System.Drawing;

    public class GridPictureBox : PictureBox
    {

        public Size GridSize
        {
            get { return my_GridSize; }
            set
            {
                my_GridSize = value;
                this.Invalidate();
            }
        }

        private Size my_GridSize = new Size(8, 8);
        public Color GridColor
        {
            get { return my_GridColor; }
            set
            {
                my_GridColor = value;
                this.Invalidate();
            }
        }

        private Color my_GridColor = Color.Black;
        protected override void OnSizeModeChanged(EventArgs e)
        {
            this.Invalidate();
            base.OnSizeModeChanged(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            this.Invalidate();
            base.OnSizeChanged(e);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics gr = pe.Graphics;

            base.OnPaint(pe);

            if (Image != null)
            {
                //get Image-Location and -Size in Picturebox
                Point p = default(Point);
                Size s = default(Size);
                switch (SizeMode)
                {
                    case PictureBoxSizeMode.Normal:
                    case PictureBoxSizeMode.StretchImage:
                    case PictureBoxSizeMode.CenterImage:
                        s = this.Size;
                        p = new Point(0, 0);
                        break;
                    case PictureBoxSizeMode.AutoSize:
                        s = Image.Size;
                        p = new Point(0, 0);
                        break;
                    case PictureBoxSizeMode.Zoom:
                        float factor = Math.Max(Convert.ToSingle(Image.Height) / Convert.ToSingle(this.Height), Convert.ToSingle(Image.Width) / Convert.ToSingle(this.Width));
                        s = new Size(Convert.ToInt32(Convert.ToSingle(Image.Width) / factor), Convert.ToInt32(Convert.ToSingle(Image.Height) / factor));
                        p = new Point((this.Width - s.Width) / 2, (this.Height - s.Height) / 2);
                        break;
                }


                //draw Grid-Lines (vertical)
                for (int x = 0; x <= s.Width; x += my_GridSize.Width)
                {
                    gr.DrawLine(new Pen(my_GridColor, 1), x + p.X, p.Y, x + p.X, p.Y + s.Height);
                }

                //draw Grid-Lines (horizontal)
                for (int y = 0; y <= s.Height; y += my_GridSize.Height)
                {
                    gr.DrawLine(new Pen(my_GridColor, 1), p.X, y + p.Y, p.X + s.Width, y + p.Y);
                }
            }

        }
    }
 
Share this answer
 
Comments
Mallesh Shantagiri 9-May-17 5:28am    
@Ralf Meier , In my above code , int j and int K are the location of picture box , by keeping j as constant and increasing K value , i can create one column of rectangles on top of image(picture box image), Now i need a logic to draw the rectangles which should cover whole picture box based on size of picture box ( using graphics in c#), .for example picture box width 600 and height 400 , i want to draw 24 rectangles on it , how can i do that . ( picture box receives live images as input) . i hope you got my problem , its very simple , i just need a for loop to draw rectangles in it , but i'm not getting how to write that logic.
Ralf Meier 9-May-17 6:01am    
Have you tried and tested my Solution ?
It draws a Grid (like you described) on the Image which is put into the PictureBox (with respecting the SizeMode-Property of the PictureBox). I think that is what you want to have - case else : what (exactly) should be changed ?
Mallesh Shantagiri 12-May-17 8:41am    
thank you @Ralf meier

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