2015-05-09 73 views
0

我試圖繪製一個PictureBox的數組,用於測試我爲每個picturebox使用相同的圖片。在vb.net中繪製一個PictureBox的數組

但不是顯示圖片,它顯示藍色。

我會告訴你一個圖片,但我沒有信譽10 ..

 Dim teren(120) As PictureBox 
     Dim x_locatie As Integer = 1, y_locatie As Integer = 0 
     For i = 0 To 10 
     x_locatie = 210 
     For j = 0 To 12 
      teren(i * j) = New PictureBox() 
      teren(i * j).Size = New Size(61, 61) 
      teren(i * j).Name = "x" + i.ToString + "y" + j.ToString 
      teren(i * j).Location = New Point(x_locatie, y_locatie) 
      Dim locatie As String = folder + "\harta\test.png" 
      teren(i * j).ImageLocation = locatie 
      teren(i * j).Show() 
     Next 
     y_locatie += 61 
    Next 

我也嘗試過其他方法,但結果相同。

Sub PictureBox1_Paint(sender1 As Object, er As PaintEventArgs) 
    If myImage IsNot Nothing Then 
     Dim r As New Rectangle(x, y, xlatime, ylungime) 
     er.Graphics.DrawImage(myImage, r) 
    End If 
End Sub 

Sub deseneaza(ByVal poza As String, ByRef x_perm As Integer, ByRef y_perm As Integer, ByRef lungime As Integer, ByRef latime As Integer) 
    myImage = Image.FromFile(poza) 
    x = x_perm 
    y = y_perm 
    xlatime = latime 
    ylungime = lungime 
    Refresh() 
End Sub 

'this part of code is in body of another function 
Dim x_locatie As Integer = 1, y_locatie As Integer = 0 
     For i = 0 To 10 
     x_locatie = 210 
     For j = 0 To 12 
      Dim locatie As String = folder + "\harta\test.png" 
      deseneaza(locatie, x_locatie, y_locatie, 61, 61) 
     Next 
     y_locatie += 61 
    Next 

我在其他線程,他們的問題的解決方案是類似的東西Dim teren() As PictureBox {teren1, teren2 , ... , teren n}但在我的情況下,問題是,我需要120個PictureBoxes看見了,我認爲它必須是一個辦法做到這一點,而無需編寫120個pictureboxes 。

+0

這似乎有點控制沉重。你可以使用一個List(矩形)列表,併爲它們繪製圖像。內存佔用更低。 – OneFineDay

+0

@OneFineDay但我必須寫出120個矩形,對吧? –

+0

儘可能多的你需要! – OneFineDay

回答

0

我認爲GDI+將是要走的路。我認爲你需要一個具有矩形結構的自定義類作爲其他屬性的成員,這些屬性可以幫助你進一步處理與角色相交的邏輯。應該在所使用的曲面控制的Paint事件中完成繪製 - PictureBox具有最佳的渲染 - IMO。

Public Class Tile 
    Public Property Bounds As New Rectangle 
    Public Property IsImpassable As Boolean 
    'others you think of 
End Class 
+0

我明白,非常感謝你,我認爲我將使用另一種編程語言。 –

+1

這將如何幫助你? – OneFineDay

+0

差異平臺如何? [XNA](https://msdn.microsoft.com/en-us/library/bb203894.aspx) – OneFineDay