Grid on picturebox

jimday1982

Member
Joined
Jul 30, 2003
Messages
22
Location
Va Beach
Does anyone know of an easy way to make a grid appear on a picture box that already has a picture on it with the press of a button. I am having trouble drawing it correctly due to varying picture sizes. Anyone have any experience with this? Thank you!
 
In the picturebox paint event I loop throught the number
Code:
    Dim Line1 As PointF
    Dim Line2 As PointF
    Dim ColSize As Single
    Dim RowSize As Single
    Dim nRows As Integer
    Dim nCols As Integer
    Dim penBlack As New Drawing.Pen(System.Drawing.Color.Black, 1)

    nRows = 9
    nCols = 18

    RowSize = Me.PictureBox1.Height / nRows
    ColSize = Me.PictureBox1.Width / nCols

     Draw Vertical grid
    Line1.X = ColSize
    Line1.Y = 0
    Line2.X = ColSize
    Line2.Y = Me.PictureBox1.Height
    For X = 0 To nCols
      e.Graphics.DrawLine(penBlack, Line1, Line2)
      Line1.X = Line1.X + ColSize
      Line2.X = Line2.X + ColSize
    Next X

     Draw Horizontal grid
    Line1.X = 0
    Line1.Y = RowSize
    Line2.X = Me.PictureBox1.Width
    Line2.Y = RowSize
    For X = 0 To nRows
      e.Graphics.DrawLine(penBlack, Line1, Line2)
      Line1.Y = Line1.Y + RowSize
      Line2.Y = Line2.Y + RowSize
    Next X
Have to do it on the paint event or you wont see it. If something in the grid changes, must call PictureBox1.Invalidate() to repaint the picturebox grid.
 
Take a look at the ControlPaint class. It has a DrawGrid method which will probably be a lot faster.
 
Suggestion: Draw the grid onto a separate in-memory bitmap (that you keep, so you only have to do it once) then draw the bitmap over the top of the existing image. Possibly use a colormatrix to set a transparent background colour.
 
Back
Top