Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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!
Posted

In the picturebox paint event I loop throught the number


   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 won't see it. If something in the grid changes, must call PictureBox1.Invalidate() to repaint the picturebox grid.

rustyd
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...