如何使用来自picturebox数组的for循环显示部分图像

时间:2013-02-19 10:44:54

标签: arrays graphics vb.net-2010 picturebox

可能有一个简单的答案,但我已经尝试了很多东西,似乎没有任何工作,除非我只是手动显示图像,但我想要的是使用这个子的其他图片,我会稍后修复一旦我让它显示这些图像。我的代码如下

Private Sub updateStandardToolbar()
    'Draws the sprites to the pictureboxes
    Dim bit As New Bitmap(gridSize, gridSize)
    Dim dest_rect As Rectangle = New Rectangle(0, 0, gridSize, gridSize)
    Dim i As Integer = 0

    ' Associate a Graphics object with the Bitmap
    Dim gr As Graphics = Graphics.FromImage(bit)

    For y As Integer = 0 To standardNum.Y
        For x As Integer = 0 To standardNum.X
            Call getSrcRect(x, y)

            'Copy that part of the image.
            gr.DrawImage(bmpStandardTile, dest_rect, src_rect, GraphicsUnit.Pixel)

            'Display the result.
            pb(i).Image = bit

            i += 1
        Next
    Next
End Sub

此代码的问题是所有图片框(pb())显示相同的图片,这是我图片上的最后一张图片。 StandardNum.x和standardNum.y是分别保存3和1的变量。 bmpStandardTile是我要复制的图像,希望dest_rect和src_rect是自解释的=)。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

喔。在查看为什么图片框显示相同的图像后,我才找到了自己问题的答案。这是我的代码以及如果其他人有类似或相同的问题我如何修复它。

Private Sub updateStandardToolbar()
    'Draws the sprites to the pictureboxes
    Dim bit As Bitmap
    Dim dest_rect As Rectangle = New Rectangle(0, 0, gridSize, gridSize)
    Dim i As Integer = 0

    ' Associate a Graphics object with the Bitmap
    'Dim gr As Graphics

    'For u As Integer = 0 To maxPics
    '    bit = New Bitmap(gridSize, gridSize)
    'Next

    For y As Integer = 0 To standardNum.Y
        For x As Integer = 0 To standardNum.X
            bit = New Bitmap(gridSize, gridSize)
            Dim gr As Graphics = Graphics.FromImage(bit)
            Call getSrcRect(x, y)

            'Copy that part of the image.
            gr.DrawImage(bmpStandardTile, dest_rect, src_rect, GraphicsUnit.Pixel)

            'Display the result.
            pb(i).Image = bit

            i += 1
        Next
    Next
End Sub

答案其实非常简单。傻我。我忘了清除位图图像,因此它不会改变最后一个图片框正在使用的图像。

bit = New Bitmap(gridSize, gridSize)

无论如何从我的错误中学到了=)