Creating and Drawing to an Off-Screen Bitmap

This topic provides an example that shows the creation of a bitmap, the creation of an off-screen graphics device and graphics context for it, and the use of that graphics context to draw to the bitmap. Once a graphics context has been established for the bitmap, any of the GDI drawing functions can be used on the bitmap, as if it were the more common graphics context—the screen.

// Create a bitmap to be used off-screen.
CFbsBitmap* offScreenBitmap = new (ELeave) CFbsBitmap();
CleanupStack::PushL(offScreenBitmap);
User::LeaveIfError(offScreenBitmap->Create(TSize(100,100),EColor256));
     
// Create an off-screen device and context.
CGraphicsContext* bitmapContext=NULL;
CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL(offScreenBitmap);
CleanupStack::PushL(bitmapDevice);
User::LeaveIfError(bitmapDevice->CreateContext(bitmapContext));
CleanupStack::PushL(bitmapContext);
     
// Draw something on the bitmap.
TRect rect(0,0,100,100);
bitmapContext->SetBrushColor(KRgbRed);
bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
bitmapContext->DrawRect(rect); // a filled red rectangle
     
// Now do what you want with it, such as blitting to the screen.
gc.BitBlt(TPoint(20,20),offScreenBitmap);
    
// Cleanup.
CleanupStack::PopAndDestroy(3);
...

Related concepts