TIPCORNER: GETTING THE DIMENSIONS OF A BITMAP

© 2002 by Alyce Watson

Home

Use of Color in Graphics

Bitmap Graphics Tutorial

Bitmap Color Formats

Bmp Dimensions

SCAN vs WAIT

Text Input Boxes

An Easy Calendar Control

Shareware Marketing

Date/Time Picker

Text Line-Wrap

Combining Commands

Newsletter help

Index


It is sometimes helpful to know the width and height of a bitmap. There are two ways to get this information. The first is to open the file and parse the first part of it to determine the dimensions. The second way requires that the bitmap be loaded into memory. The dimensions can then be retrieved with an API call.

OPENING FILE:

We can get the dimensions of a bitmap on disk with Liberty BASIC commands. We can open the file and read the first part of it into a string variable, then parse the string variable to find the width and height of the bitmap.

Here is the simple code to get a bitmap's dimensions. This technique is explained in detail in Newsletter #67, so please refer to that issue for more information on reading file headers. The code below can be used just as it is (even if you don't understand it!) to get the dimensions of a bitmap on disk.

filedialog "Open Bitmap","*.bmp", picFile$

open picFile$ for input as #pic
    pic$=input$(#pic, 29)
close #pic

bmpwidth = asc(mid$(pic$,19,1)) + (asc(mid$(pic$,20,1)) * 256)
bmpheight = asc(mid$(pic$,23,1)) + (asc(mid$(pic$,24,1)) * 256)

print "Bitmap width is ";bmpwidth
print "Bitmap height is ";bmpheight

USING API CALLS:

We can also get the dimensions of a loaded bitmap with an API call to GetObjectA. It requires a handle to the loaded bitmap. We get the handle with the HBMP() function. It also requires a struct to hold the values returned by the function. After the function is called, the width and height are contained in the designated members of the BITMAP struct. Here is the usage:

'load a bitmap from disk into memory:
loadbmp "MyBmp", bmpfile$

'get the bitmap handle with HBMP()
hBitmap=hbmp("MyBmp")

'create a struct to hold the returned values:
    struct BITMAP,_
    bmType As long,_
    bmWidth As long,_
    bmHeight As long,_
    bmWidthBytes As long,_
    bmPlanes As word,_
    bmBitsPixel As word,_
    bmBits As long

'get the size of the struct:
    nSize=Len(BITMAP.struct)

'make the call to GetObjectA:
    CallDLL #gdi32, "GetObjectA",_
        hBitmap As long,_    'handle of bitmap
        nSize As long,_      'size of struct
        BITMAP As struct,_   'name of struct
        results As long

'retrieve the dimensions of the loaded bitmap:
    BitmapWidth=BITMAP.bmWidth.struct
    BitmapHeight=BITMAP.bmHeight.struct

'unload the bitmap from memory:
    unloadbmp "MyBmp"


Home

Use of Color in Graphics

Bitmap Graphics Tutorial

Bitmap Color Formats

Bmp Dimensions

SCAN vs WAIT

Text Input Boxes

An Easy Calendar Control

Shareware Marketing

Date/Time Picker

Text Line-Wrap

Combining Commands

Newsletter help

Index