PictureBox不显示动态生成的位图

时间:2014-01-12 17:35:41

标签: vb.net picturebox

在Visual Basic .NET Windows窗体应用程序中,我从原始EGA数据创建Bitmap对象并尝试在PictureBox控件中显示它们。它可以很好地创建对象,因为我通过使用立即窗口来调用GetPixel来确定。 SixteenColorBitmap是我正在使用的库中的一个类。

Function TileImage(Tile As SixteenColorBitmap) As Bitmap
    Dim b As New Bitmap(32, 32)
    For y = 1 To 16
        For x = 1 To 16
            Dim t = Tile.Color(x - 1, y - 1)
            Dim c As Color = Drawing.Color.FromArgb(RGB(t.Item1, t.Item2, t.Item3))
            b.SetPixel((x - 1) * 2, (y - 1) * 2, c)
            b.SetPixel((x - 1) * 2 + 1, (y - 1) * 2, c)
            b.SetPixel((x - 1) * 2, (y - 1) * 2 + 1, c)
            b.SetPixel((x - 1) * 2 + 1, (y - 1) * 2 + 1, c)
        Next
    Next
    Return b
End Function

加载EGA图形时,为每个图块调用该图形,并将结果存储在列表中。当用户选择一个图块时,它应该从该列表中拉出右图块ID并将其分配给该PictureBox的Image属性,如下所示:

TileBackground.Image = BackgroundTiles(SelTile(0))

TileBackground是PictureBox,BackgroundTiles是GDI +位图的列表,SelTile是所选磁贴的数组。)当该代码运行时,我确信它是这样做,位图被分配给属性,但即使我调用InvalidateRefresh,它也不会显示在表单上。

1 个答案:

答案 0 :(得分:0)

好吧,我最终确实让它运转了。正如Jens所怀疑的那样,生成的Bitmap出现了问题,这导致PictureBox惊慌失措(无声)而不显示它。由于某些未知原因,以下代码有效:

Function TileImage(Tile As SixteenColorBitmap) As Bitmap
    Dim b As New Bitmap(16, 16)
    For y = 1 To 16
        For x = 1 To 16
            Dim t = Tile.Color(x - 1, y - 1)
            Dim c As Color = Drawing.Color.FromArgb(255, t.Item1, t.Item2, t.Item3)
            b.SetPixel(x - 1, y - 1, c)
        Next
    Next
    Return b
End Function

我没有手动进行比例,而是让32x32控件显示我的16x16图像。