How to Display a Bitmap?

How do you display a bitmap?

There are several ways to display a bitmap in a form. The most common are:

 

  1. Include the bitmap as an element on a form and then just display the form. This is how you do it if the bitmap won't change based on context, and it's the most common way.
  2. Declare the bitmap as an unattached bitmap resource and then display it under program control at the appropriate time and place. This is necessary if you want to display different bitmaps based on context, or do simple low speed animation.
  3. Use the Palm OS drawing functions to draw the necessary pixels directly onto the screen. This is if you need basic dynamic bitmap creation.
  4. Manipulate video memory directly. This is what you'll have to do if you want to do fast and smooth animation, or if you want to do fancy stuff like fades, merges, etc. Note that this is very hard to get right, especially as screen technologies change. Today it's color bit depth changing every six months, tomorrow it'll be screen resolution or some other thing.
Options 1 and 3 are no-brainers and option 4 is beyond the scope of this document. There is sample code available on the net somewhere that demonstrates #4 if you need it.

The following code illustrates option 2:

 

      boolean DrawBitmap(int iResourceID, int x, int y)
{
VoidHand hResource; // Generic Handle to resource

if((hResource = DmGet1Resource(bitmapRsc, iResourceID)) != 0)
{
BitmapPtr pzBitmap = (BitmapPtr) MemHandleLock(hResource);
WinDrawBitmap(pzBitmap, x, y);
MemPtrUnlock(pzBitmap);
DmReleaseResource(hResource);
return(true);
}

else
{
// Put Your Error Code here for no bitmap found
return(false);
}
}

Here's what the code does:

It first calls DMGet1Resource() to get a handle to the resource. It then calls MemHandleLock() to lock the bitmap resource's memory down and returns a pointer to the data. The next step is to draw the bitmap with WinDrawBitmap() which requires a pointer to the bitmap. Next you need to unlock the memory associated with the bitmap by pointer using MemPtrUnlock() and then release the resource by handle using DmReleaseResource().

Today: Sep 8, 2010