VBfx / Tile tutorial / Step 4

Loadig bitmaps

The function will first check if the bitmap's already loaded and if so just return the bitmaps index in our array. This check is done by comparing the file names we stored in our bitmap Type. The same loop also checks if a item was unloaded, if so exiting the loop so we can use this slot to load the new bitmap into.

(I splitted up the loading function into logial parts, you need to put it all together to make it work)

Public Function LoadBitmap( iFileName as String ) as Long
On Error GoTo 1

    Dim A as Long
    'Check if already loaded
    For A = 0 To BitmapCount
        If Bitmap( A ).FileName = LCase( iFileName ) Then
            'Bitmap already loaded
            LoadBitmap = A

            Exit Function
        End If
        'Use this slot if its empty
        If Len( Bitmap( A ).FileName ) = 0 Then: Exit For

When we reach this point in code, there was no file name matching so we have to load the picture now. First we check if the specified file exists. This is done by calling the Dir() function. If the function fails we return -1 and we're done already.

    'Check filename
    If Dir( iFileName, vbNormal ) = "" Then
        'Return -1 to show an error
        LoadBitmap = -1
        Exit Function
    End If

Here again we check if the loop above found a empty slot we can load the new bitmap into. If not the code allocates another item for the new bitmap:

    'If no empty slot found
    If A > BitmapCount Then
        'Allocate memory for the new bitmap
        BitmapCount = BitmapCount + 1
        ReDim Preserve Bitmap( BitmapCount )
    End If

Now that all checks are done we can straightly load the file into the array. Note that we store the DC, PictureDisp and the OriginalObject into our bitmap Type so we can later release it correctly. You don't have to understand what the 3 API calls really do, just remember that we can't just get the DC and then forget about it.

    With Bitmap( A )
        With .Picture
            'Setup bitmap
            .MemoryDC = False
            'Load picture and set object
            .DC = CreateCompatibleDC( MainWindow.hDC )
            Set .PictureDisp = LoadPicture( iFileName )
            .OriginalObject = SelectObject( .DC, .PictureDisp )
        End With

Since the file is loaded now we can set the file name and get the picture size. In case of an error (eg. if the file was broken) the code wouldn't even reach this point, leaving the .FileName property blank to make this slot available in further calls.

        .FileName = LCase( iFileName )
        'Get size in pixel
        .w = MainWindow.ScaleX( .Picture.PictureDisp.Width )
        .h = MainWindow.ScaleY( .Picture.PictureDisp.Height )
    End With

Finally the function returns the index of the new item. We can use this index later to access the bitmap and get it's DC if we need.

    'Return index
    LoadBitmap = BitmapCount
    Exit Function

Some error handling follows, this is just in case there was an error loading the bitmap. The code will then call UnloadBitmap to free any resources that are probably assigned already and return -1 to indicate a error.

1  'Error
    UnloadBitmap Bitmap( A )
    'Return error
    LoadBitmap = -1
End Function


That was quite a bunch of code but except the loading part this should have been easy. As already mentioned you don't have to exactly know what each API call does. It's nice if you do but more important to know how you can use it in your program.