::LoadImageSee also How To Use LoadImage() to Read a BMP file (with pallette support)
*pIconHandle = 0;
HBITMAP hbitmap;
hbitmap = (HBITMAP)LoadImage(0, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
if ( hbitmap == 0) {
return E_FAIL;
}
DIBSECTION dibSection;
if ( ::GetObject( hbitmap, sizeof(dibSection), &dibSection) == 0) {
::DeleteObject( hbitmap);
return E_FAIL;
}
HDC hdcDisplay = ::CreateDC( _T("DISPLAY"), NULL, NULL, NULL);
HDC hdcBitmap = ::CreateCompatibleDC( hdcDisplay);
BITMAPINFO *pSrcBmi = (BITMAPINFO*)(new char[ sizeof(*pSrcBmi) + 4 * 256]);
memcpy( pSrcBmi, &(dibSection.dsBmih), sizeof(pSrcBmi->bmiHeader));
if ( dibSection.dsBm.bmBitsPixel * dibSection.dsBm.bmPlanes <= 8) {
// has a palette
HDC hMemDc;
HBITMAP hOldBitmap;
RGBQUAD rgb[256];
hMemDc = CreateCompatibleDC( NULL);
hOldBitmap = (HBITMAP)::SelectObject( hMemDc, hbitmap);
GetDIBColorTable( hMemDc, 0, 256, rgb);
for ( int i = 0; i < 256; i++) {
pSrcBmi->bmiColors[i].rgbRed = rgb[i].rgbRed;
pSrcBmi->bmiColors[i].rgbGreen = rgb[i].rgbGreen;
pSrcBmi->bmiColors[i].rgbBlue = rgb[i].rgbBlue;
pSrcBmi->bmiColors[i].rgbReserved = 0;
}
::SelectObject( hMemDc, hOldBitmap);
DeleteDC( hMemDc);
}
BITMAPINFO targetBmi;
ZeroMemory( &targetBmi, sizeof(targetBmi));
targetBmi.bmiHeader.biSize = sizeof(targetBmi);
targetBmi.bmiHeader.biWidth = dibSection.dsBm.bmWidth;
targetBmi.bmiHeader.biHeight = dibSection.dsBm.bmHeight;
targetBmi.bmiHeader.biPlanes = 1;
targetBmi.bmiHeader.biBitCount = 32;
unsigned char *pDibData = 0;
HBITMAP hBitDib;
hBitDib = ::CreateDIBSection( hdcBitmap, &targetBmi, DIB_RGB_COLORS, (void**)(&pDibData), NULL, 0);
HGDIOBJ tmpObj = ::SelectObject( hdcBitmap, hBitDib);
int retcode;
retcode = StretchDIBits(
hdcBitmap,
0,0,
dibSection.dsBm.bmWidth,
dibSection.dsBm.bmHeight,
0,0,
dibSection.dsBm.bmWidth,
dibSection.dsBm.bmHeight,
dibSection.dsBm.bmBits,
pSrcBmi,
DIB_RGB_COLORS,
SRCCOPY
);
// pDibData now contains the 32 bit pixel data from the bmp
::SelectObject( hdcBitmap, tmpObj);
::DeleteObject( hBitDib);
::ReleaseDC( NULL, hdcBitmap);
::ReleaseDC( NULL, hdcDisplay);
::DeleteObject( hbitmap);
*pIconHandle = m_pSS->m_handleManager.GetNextHandle( pBit);
return S_OK;
HDC hdcDesktop = ::CreateDC( _T("DISPLAY"), NULL, NULL, NULL);
COLORREF color;
color = ::GetPixel( hdcDesktop, int xPosLogical, int yPosLogical);
::DeleteDC( hdcDesktop);
HBRUSH hBrush = ::CreateSolidBrush( RGB(0xff, 0x7f, 0x00)); HBRUSH hOld = ::SelectObject( hDc, hBrush); // Use it Here ... ::SelectObject( hDc, hOld); ::DeleteObject( hBrush);
HBITMAP hBitDib;
// Get the display DC
HDC hddc = ::CreateDC( _T("DISPLAY"), NULL, NULL, NULL);
// Make the DC to manipulate the bitmap
HDC dcMem = ::CreateCompatibleDC( hddc);
// (200,100) 24 bit bitmap
BITMAPINFO bmi;
ZeroMemory( &bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(bmi);
bmi.bmiHeader.biWidth = 200;
bmi.bmiHeader.biHeight = 100;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
// pBitsDib is the pointer to the bytes in the bitmap
unsigned char *pBitsDib = 0;
hBitDib = ::CreateDIBSection( dcMem, &bmi, DIB_RGB_COLORS, (void**)(&pBitsDib), NULL, 0);
// Make the bitmap be the drawing surface for dcMem
HGDIOBJ tmpObj = ::SelectObject( dcMem, hBitDib);
// Draw something into it
Rect pos = { 0, 0, 0, 0 };
pos.right = 200;
pos.bottom = 100;
::FillRect( dcCopy, &pos, (HBRUSH) ::GetStockObject(GRAY_BRUSH));
// Finish
::SelectObject( dcMem, tmpObj);
::ReleaseDC( dcMem);
// Note DeleteObject also frees the memory for the bits (unless
// you allocated it yourself)
::DeleteObject( hBitDib);
| ::LoadImage | Load an image from a FILE (?or a resource?) | |
| ::GetObject | Get a BITMAP or DIBSECTION struct from a bitmap handle | BITMAP bm; |
| ::GetDIBits | Gets the Bits of a hBitmap (?and converts color depth?) |
| Function | Description |
|---|---|
HBITMAP CreateCompatibleBitmap( | SelectObject( HDC, HBITMAP); |
CreateCompatibleDC( | |
CreateFont( | |
LONG GetBitmapBits( HBITMAP hbmp, LONG cbBuffer, LPVOID lpvBits); | |
int GetDIBits( HDC, HBITMAP | |
HGDIOBJ SelectObject( HDC, HGDIOBJ) | |
TextOut( |
| GetTextExtentPoint32 | Gets Size needed to render a string in Logical Units |
| HGDIOBJ SelectObject( HDC, HGDIOBJ hFont) | returns hOldFont, or NULL if failure |
To use an object (Bitmap, Brush, Font, Pen, Region), you must select it into a DC through this:
HGDIOBJ SelectObject( HDC, HGDIOBJ);
It returns the previous Object
| Object | Description | Release Method |
|---|---|---|
| HDC | Handle to Device Context | DeleteDC( HDC) |
| HBITMAP | Handle to Bitmap | DeleteObject( {object}) |
| HFONT | Handle to Font | DeleteObject( {object}) |