在vb.net中绘制PictureBoxes数组

时间:2015-05-09 15:45:36

标签: vb.net

我正在尝试绘制一个PictureBoxes数组,为了测试我为每个图片框使用相同的图片。

但不是显示图片,而是显示蓝色。

我会给你看一张照片,但我没有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个图片盒

2 个答案:

答案 0 :(得分:1)

请尝试此操作...它将连续生成16个图片框,大小为20x20。我将其放在“ FormLoading”事件下。

        Dim Shapes(16) As PictureBox
        For i = 1 To 16
            Shapes(i) = New PictureBox
            With Shapes(i)
                .BackColor = SystemColors.Control 'Color.Green
                .BackgroundImage = New Bitmap(My.Resources.led_blk)
                .BackgroundImageLayout = ImageLayout.Zoom
                .Size = New Size(20, 20)
                .Visible = True
                .Location = New Point( 23 * i, 50)
            End With
            Me.Controls.Add(Shapes(i))
      Next

答案 1 :(得分: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