2012-08-02 67 views
2

我想在picturebox上顯示圖像,並且我想在picturebox上顯示網格。所以,如果我放大圖像,那麼我可以很容易地識別出picturebox上的像素大小。任何一個都可以幫助我做到這一點?例如。如何在圖片框控件上顯示網格?

圖像上的PictureBox

enter image description here

這正常顯示,但我想在PictureBox中這樣顯示圖像

enter image description here

+0

[''Graphics.DrawLine()'](http://msdn.microsoft.com/en-us/library/zd7xsffw.aspx) – 2012-08-02 06:34:24

+1

哪種語言? .Net還是經典的vb? – 2012-08-04 17:34:53

回答

2

下面的代碼繪製使用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 

要測試這個,創建一個新項目並添加一個picturebox(名稱= PictureBox1)。然後爲它選擇一個圖像(您可以使用屬性窗口設置圖像)。然後複製粘貼上面的代碼並運行它。你會看到網格線。我們已經編寫了代碼以在Picturebox的paint事件上繪製網格線。因此,當您在運行時在圖片框上設置圖像時,這些網格也將重新繪製。

希望它會給你一個想法。順便說一句,以上是使用VB.Net編碼和測試。 祝你好運...

+0

它工作正常.. :-) – 2012-08-16 08:19:38

+1

不客氣.. :) – 2012-08-16 17:47:40

+0

@AkhileshBChandran我如何繪製1px間距的網格線? – Amir 2012-12-10 21:08:32