【问题标题】:PictureBox doesn't display dynamically-generated BitmapPictureBox 不显示动态生成的位图
【发布时间】:2014-01-12 17:35:41
【问题描述】:

在 Visual Basic .NET Windows 窗体应用程序中,我从原始 EGA 数据创建位图对象并尝试在 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

【问题讨论】:

  • 您是否尝试BackgroundTiles(SelTile(0)).Save() 来查看从光盘生成的位图以进行调试?如果真的是图片框功能失常,那可能会清除。

标签: vb.net picturebox


【解决方案1】:

嗯,我最终确实让它工作了。正如 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 图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多