Praveen Kumar
Praveen Kumar

Reputation: 1658

How to show a grid on picturebox control?

I want to display a image on picturebox and also i want to show grid on picturebox.So if i zoom the image then i can easily identify the pixel size on picturebox.Can any one help me to do this?For eg.

This normal display of image on picturebox

enter image description here

But i want to display image like this on picturebox

enter image description here

Upvotes: 2

Views: 4425

Answers (1)

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

Here's the code to draw grid lines using the Graphics.DrawLine():

Public Class Form1

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        Dim g As Graphics = e.Graphics
        Dim pn As New Pen(Color.White) '~~~ color of the lines

        Dim x As Integer
        Dim y As Integer

        Dim intSpacing As Integer = 10  '~~~ spacing between adjacent lines

        '~~~ Draw the horizontal lines
        x = PictureBox1.Width
        For y = 0 To PictureBox1.Height Step intSpacing
            g.DrawLine(pn, New Point(0, y), New Point(x, y))
        Next

        '~~~ Draw the vertical lines
        y = PictureBox1.Height
        For x = 0 To PictureBox1.Width Step intSpacing
            g.DrawLine(pn, New Point(x, 0), New Point(x, y))
        Next

    End Sub

End Class

To test this, create a new project and add a picturebox (name = PictureBox1). Then select an image for it(you could use the property window to set the image). Then copy-paste the above code and run it. You will see grid lines. We have written the code to draw the gridlines on the paint event of the Picturebox. So, these grids will be redrawn when you set an image on the picturebox at runtime too.

Hope it will give you an idea. BTW, the above was coded and tested using VB.Net. Wish you good luck...

Upvotes: 3

Related Questions