Quote:> I have a manual that lists windows API functions for dealing with
> bitmaps but it doesnt make any sense to me.
> (b) Explain to me in simple terms the steps required to display an
> image on the screen from a file, and how to store a bitmap from a file
> into a memory buffer, when working with BorlandC for Windows.
There are several API functions available to ease the process. I understand
though, the API confused to all to well when I first delved into Windows
programming. Fortunately, with some time and patience I was up to speed, as I'm
sure you will be. To load a bitmap, the easiest thing to do is to use the
LoadImage() API function. Here's a simple wrapper function that demonstrates
how this can be used...
HBITMAP LoadBitmapFile(char *filename)
// use the LoadBitmap() API function to load the
// bitmap file (returns a handle to the BITMAP)
return LoadImage(HInstance, filename, IMAGE_BITMAP, 0, 0,
Here, HInstance is the handle to the instance of your application. The zeros as
the fourth and fifth parameters tell the function to use the actual bitmap's
width and height. The LR_LOADFROMFILE flag tells the function that the second
parameter is a filename. The function returns a handle to a BITMAP (HBITMAP).
Ok, with the above function you can get the handle, now how to display it? For
this, you can use the BitBlt() API function. Unfortunately, this function takes
HDC parameters, not HBITMAP's. HDC is a handle to a device context, which is
simply a Windows drawing surface. You can grab a handle to a window's device
context by using the GetDC() API function, but for a bitmap, you'll need to use
a memory device context. So, what needs to be done is to create a memory device
context (via CreateCompatibleDC), select the bitmap into the device context (via
SelectObject), then BitBlt() from the memory device context to the device
context of the window you want to display the bitmap on. This takes care of the
HDC parameters of BitBlt(); the nXDest and nYDest define the location you want
to draw the bitmap at, while the nWidth and nHeight parameters specify the width
and height of the bitmap. Well, you have an HBITMAP, but you don't have its
width and height. To get these values, use the GetObject() API function which
will fill a BITMAP structure from its handle. You can read the width and height
directly from the BITMAP structure. Here's an example...
void DisplayBitmap(HWND HWnd, int X, int Y, HBITMAP HBitmap)
// use the GetObject() API function to
// retrieve the width and height of the bitmap
GetObject(HBitmap, sizeof(BITMAP), &Bitmap);
int bitmap_width = Bitmap.bmWidth;
int bitmap_height = Bitmap.bmHeight;
// create a memory device context compatible
// with the screen
HDC HMemoryDC = CreateCompatibleDC(0);
// select the bitmap into the memory device context
HBITMAP HOldBitmap = SelectObject(HMemoryDC, HBitmap);
// grab the handle to the device context of
// the window whose handle is passed in as HWnd
HDC Hdc = GetDC(HWnd);
// block transfer the bits from the
// memory DC to the screen
BitBlt(Hdc, X, Y,
HMemoryDC, 0, 0,
// clean up
So when you actually use these functions, you'll do a process similar to...
HBITMAP HBitmap = LoadBitmapFile("C:\\Windows\\Images\\Bitmap1.bmp\0");
DisplayBitmap(Handle, 5, 5, HBitmap);
Hope this helps!
Answers to <Commonly Asked Questions>