Ben N Ben N - 5 months ago 12
Vb.net Question

PictureBox doesn't display dynamically-generated Bitmap

In a Visual Basic .NET Windows Forms application, I am creating Bitmap objects from raw EGA data and trying to display them in PictureBox controls. It can create the objects fine, as I determined by using the Immediate window to call GetPixel.

SixteenColorBitmap
is a class from a library I am using.

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


On loading the EGA graphics, that is called for each tile and the result stored in a list. When the user selects a tile, it should pull the right tile ID out of that list and assign it to the
Image
property of that PictureBox, like this:

TileBackground.Image = BackgroundTiles(SelTile(0))


(
TileBackground
is a PictureBox,
BackgroundTiles
is the list of GDI+ bitmaps, and
SelTile
is the array of selected tiles.) When that code runs, as I am sure it is doing, the bitmap is assigned to the property, but it does not display on the form, even when I call
Invalidate
or
Refresh
.

Answer

Well, I did eventually get it working. As Jens suspected, something was wrong with the generated Bitmap, which was causing the PictureBox to freak out (silently) and not display it. The following code, for some unknown reason, works:

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

Instead of manually doing the scale, I have the 32x32 control display my 16x16 image.

Comments