Drawing Bitmaps

This topic provides example code that demonstrates blitting a bitmap to the screen, blitting a rectangular piece of a bitmap to the screen and blitting a bitmap under a mask.

These examples assume that bitmap is a pointer to a valid CFbsBitmap object.

Bitmap block transfer

Use CBitmapContext::BitBlt() to blit a CFbsBitmap to the screen.

// draw the bitmap using bitmap block transfer
TPoint pos(50,50);
gc.BitBlt(pos, bitmap);

Block transfer of a rectangular piece of a bitmap

You can blit a rectangular piece of a bitmap to the screen.

...
// set a rectangle for the top-left quadrant of the source bitmap
TSize bmpSizeInPixels=bitmap->SizeInPixels();
TSize bmpPieceSize(bmpSizeInPixels.iWidth/2,bmpSizeInPixels.iHeight/2);
TRect bmpPieceRect(TPoint(0,0),bmpPieceSize); 
    
// blit only the piece of the bitmap indicated by bmpPieceRect
TPoint pos(100,100);
gc.BitBlt(pos, bitmap, bmpPieceRect);

Masked bitmap block transfer

Masks can be used to select which parts of a bitmap are drawn by CBitmapContext::BitBltMasked().

Masks can be used to not display pixels of the source bitmap if their corresponding mask pixel is black, or, alternatively, where the mask is white (called an inverted mask).

The following figure shows successively a source bitmap, a mask, and the outcome when they are blitted with BitBltMasked().

// Load the mask bitmap, just like any other
CFbsBitmap* maskBitmap = new (ELeave) CFbsBitmap();
CleanupStack::PushL(maskBitmap);
User::LeaveIfError(maskBitmap->
  Load(multiBitmapFile,EMbmGrbmap2Smilmask));
    
// Calculate rectangle for the whole bitmap
TRect bmpPieceRect(TPoint(0,0),bitmap->SizeInPixels());
    
// Blit using a masking bitmap, with no inversion
gc.BitBltMasked(TPoint(50,50),bitmap,bmpPieceRect,maskBitmap,EFalse);
    
...
// clean up
CleanupStack::PopAndDestroy();

Notes

  • Unlike with an ordinary BitBlit(), with BitBltMasked() you always have to specify what part of the bitmap to display: here we just set bmpPieceRect so that the whole bitmap is displayed.

  • If the mask bitmap is smaller than the source bitmap, then it is tiled across the bitmap.

  • For an inverted mask, set the last argument to BitBltMasked() to ETrue.

Related concepts