Drawing Reduced and Enlarged Bitmaps

This topic provides examples of using CGraphicsContext::DrawBitmap() to draw a bitmap that is stretched or compressed.

The bitmap blitting functions of the bitmapped graphics context (CBitmapContext) cannot be used to perform scaling of the bitmap. They always draws the bitmap using its dimensions in pixels. The overloaded GDI CGraphicsContext::DrawBitmap() function, however, can be used to draw a bitmap that is:

  • stretched or compressed to a set size in twips

  • stretched and/or compressed to fit a given rectangle.

Note: DrawBitmap() is slower than BitBlt(). You should therefore use BitBlt() in preference wherever possible.

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

Draw a bitmap at a set size in twips

A bitmap can have a real world size in twips set for it, and be drawn scaled to that size.

  1. Construct a TSize with the required dimensions in twips.

  2. Set the size of the bitmap using CFbsBitmap::SetSizeInTwips(). The default size in twips of a bitmap when loaded is (0,0).

  3. Draw the bitmap using DrawBitmap()

// Set twips size 
TSize bmpSizeInTwips(2000,2000);
bitmap->SetSizeInTwips(bmpSizeInTwips);
gc.DrawBitmap(TPoint(50,50), bitmap);

Draw a bitmap in a given rectangle

DrawBitmap() can also draw a bitmap by stretching it to fit a given rectangle.

// Draw bitmap stretched from the origin to 100,100
TRect rect(0,0,100,100);
gc.DrawBitmap(rect, bitmap);

Related concepts