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

时间:2022-09-27 08:59:15

There may be a simple answer but i have tried quite a few things and nothing seems to work unless i just manually display the images but what i want is to use this sub for other pictures as well which i will fix later once i get it to display these images. My code is as follows

可能有一个简单的答案,但我已经尝试了很多东西,似乎什么都没有用,除非我只是手动显示图像,但我想要的是将这个子用于其他图片,我将在以后修复它显示这些图像。我的代码如下

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

The problem with this code is all the pictureboxes (pb()) displays the same picture which is the last one on my image. StandardNum.x and standardNum.y are variables which hold 3 and 1 respectively. bmpStandardTile is the image I want to copy from and hopefully dest_rect and src_rect is self explanatory =). Any helps is appreciated.

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

1 个解决方案

#1


0  

Oh. I just found the answer to my own problem after looking at why the pictureboxes displays the same image. Here is my code and how i fixed it if anyone else has a similar or the same problem.

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

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

The answer was actually very simple. Silly me. I forgot to clear the bitmap image so it will not change the image which the last picturebox was using.

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

bit = New Bitmap(gridSize, gridSize)

Anyway Learnt from my mistakes =)

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

#1


0  

Oh. I just found the answer to my own problem after looking at why the pictureboxes displays the same image. Here is my code and how i fixed it if anyone else has a similar or the same problem.

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

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

The answer was actually very simple. Silly me. I forgot to clear the bitmap image so it will not change the image which the last picturebox was using.

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

bit = New Bitmap(gridSize, gridSize)

Anyway Learnt from my mistakes =)

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