Working with Bitmaps in MFC
This section details how to use and manipulate bitmaps within your Microsoft Foundation Class (MFC) applications. Bitmaps are raster graphics images composed of a grid of pixels. MFC provides robust support for loading, displaying, and modifying bitmaps.
Understanding Bitmap Objects
In MFC, bitmaps are represented by the CBitmap class. This class encapsulates a Windows bitmap handle (HBITMAP) and provides member functions for creating, loading, selecting, and managing bitmap data.
Loading Bitmaps
You can load a bitmap from a resource file or from a file on disk. The CBitmap class offers several ways to do this:
CBitmap::LoadBitmap(LPCTSTR lpszResourceName): Loads a bitmap from the application's resources.CBitmap::LoadBitmap(UINT nIDResource): Loads a bitmap from the application's resources using its resource ID.CBitmap::LoadImage(LPCTSTR lpszFileName, ...)`: A more general-purpose function that can load various image types, including bitmaps, from files or resources.
Example: Loading from Resources
CBitmap myBitmap;
if (myBitmap.LoadBitmap(IDB_MY_ICON))
{
// Bitmap loaded successfully
// Now you can draw it onto a device context
}
else
{
// Handle error loading bitmap
}
Displaying Bitmaps
To display a bitmap, you typically need a CDC (Device Context) object. You can then use the CDC::BitBlt or CDC's drawing functions to transfer the bitmap's pixels to the screen or another device context.
Drawing a Bitmap on a Device Context
A common scenario is drawing a bitmap within the OnDraw member function of a view class.
void CMyView::OnDraw(CDC* pDC)
{
// Assume m_bitmap is a CBitmap member variable already loaded
if (m_bitmap.m_hObject != NULL)
{
// Create a compatible memory DC
CDC memDC;
memDC.CreateCompatibleDC(pDC);
// Select the bitmap into the memory DC
CBitmap* pOldBitmap = memDC.SelectObject(&m_bitmap);
// Get the bitmap dimensions
BITMAP bmpInfo;
m_bitmap.GetBitmap(&bmpInfo);
// Draw the bitmap onto the client DC using BitBlt
pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, 0, 0, SRCCOPY);
// Restore the original bitmap in the memory DC
memDC.SelectObject(pOldBitmap);
}
}
Bitmap Manipulation
MFC's CBitmap class allows for basic manipulation, such as retrieving dimensions and pixel data. For more advanced image processing, you might need to work directly with GDI functions or third-party libraries.
Getting Bitmap Information
BITMAP bmpInfo;
m_bitmap.GetBitmap(&bmpInfo); // Fills the BITMAP structure
int width = bmpInfo.bmWidth;
int height = bmpInfo.bmHeight;
int colorPlanes = bmpInfo.bmPlanes;
int bitsPerPixel = bmpInfo.bmBitsPixel;
Creating New Bitmaps
You can also create new bitmaps programmatically:
CBitmap::CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitcount, const void* lpBits)CBitmap::CreateCompatibleBitmap(CDC* pDC, int nWidth, int nHeight)CBitmap::CreateDiscardableBitmap(CDC* pDC, int nWidth, int nHeight)
Example: Creating a Blank Bitmap
CBitmap myNewBitmap;
// Assuming pDC is a valid CDC pointer (e.g., from GetDC())
if (myNewBitmap.CreateCompatibleBitmap(pDC, 100, 50))
{
// Successfully created a 100x50 pixel bitmap
}
Using Bitmaps with Controls
Bitmaps are frequently used with MFC controls, such as buttons (for icons) or list views. The CBitmap object can be associated with these controls to provide custom visual elements.