title: Vb.Net : Dynamically create rectangles in a picturebox side by side
body: <p>I am current working on an application in which I am using a video stream eventhandler to produce a camera image in a Forms picturebox in VB.Net (yes it sounds like a kinect app...it is...)</p>
  
  <p>I can create a red rectangle over my bitmap image (camer stream) using this code:</p>
  
  <pre><code>        Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
  
                  Dim rect As Rectangle = New Rectangle
                  rect.Width = 6.4
                  rect.Height = 480
                  g.DrawRectangle(Pens.Red, rect)
  
  
          End If
  </code></pre>
  
  <p>Straight forward enough.</p>
  
  <p>But, what I really would like to happen is to be able to create multiple rectangles side by side crossing the width of my picture box at "6.4" (&lt;--pixels I think??)</p>
  
  <p>I have attempted using a "For Loop" to create these rectangles and then setting a new location for each one, like this:</p>
  
  <pre><code>            Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
              Dim i As Integer
              For i = 0 To 100
                  Dim rect As Rectangle = New Rectangle
                  rect.Width = 6.4
                  rect.Height = 480
                  rect.Location = New System.Drawing.Point(*{not sure how to measure here}*, *{or here either}*)
                  g.DrawRectangle(Pens.Red, rect)
  
              Next
  </code></pre>
  
  <p>Any help would be wonderful.</p>
  
  <p>My plan is to use these rectangles to help me choose skeletons to monitor chosen by the microphone array sound source position. Hence the rectangles width of "6.4"</p>
  
  <p>640 is the Width resolution of the video stream, and I divided it by "100" the measure of "-50 to 50" in the microphone array. </p>
  
  <p>But, in the end I just need help with the rectangles. </p>
  
  <p><strong>EDIT: Here is something here that I have come up with. I am now very close to an answer:</strong></p>
  
  <pre><code>            Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
              Dim i As Integer = 1
              Dim newLocationX As Integer = 0
  
              Do Until i = 100
                  Dim rect(i) As Rectangle
                  Rect(i) = New Rectangle
                  Rect(i).Width = 6
                  Rect(i).Height = 480
                  Rect(i).Location = New System.Drawing.Point(video.Left + newLocationX, Rect(i).Location.Y)
                  g.DrawRectangle(Pens.Red, Rect(i))
                  newLocationX = newLocationX + 6
                  i += 1
              Loop
          End If
  </code></pre>
  
  <p><strong>Last Edit: My math was way way wrong. Here is how I got it to work:</strong></p>
  
  <p>The resolution of the image width is 640
  I needed to fill the width with 100 rectangles.</p>
  
  <p>The math is 640 / 100 = 6.4 &lt;-- the width of the rectangles</p>
  
  <p>I thought that I had to repeat the For Loop 100 times to get 100 rectangles. this is not true. Repeat the For Loop the width of the picture box: 640 times!</p>
  
  <p>The fact that you place rectangles in the picture box at a width 6.4 will automatically create 100 rectangles in a picture box with a width of 640.</p>
  
  <p><strong>here is the code that works:</strong></p>
  
  <pre><code>            Dim g As Graphics = Graphics.FromImage(kinectVideoBitMap)
              Dim i As Integer = 0
              Dim newLocationX As Integer = 0
  
              Do Until i = 640
                  Dim rect(i) As Rectangle
                  rect(i) = New Rectangle
                  rect(i).Width = 6.4
                  rect(i).Height = 480
                  rect(i).Location = New System.Drawing.Point(video.Right - newLocationX, rect(i).Location.Y)
                  g.DrawRectangle(Pens.Red, rect(i))
                  newLocationX = newLocationX + 6.4
                  i += 1
              Loop
  </code></pre>