Introduction
A more easy
to
save
your
memory
dc
to a (DIB)
bitmap
file(*.bmp),
sometimes we
don't need
to bitblt
our map to
windows to
display.
this is a
new way you
can try.
Using the
code
Only a
function we
needed
here.It's
very hard to
post whole
project file
here to
discuss. so
i suppose
the reader
have some
basic ideal
about GDI
programming.
Pass two
parameters
into the
function ,
you may need
CreateCompatibleBitmap
to init your
second
parameter.
Collapse
Copy
Code
HBITMAP CreateCompatibleBitmap(
HDC hdc,
int nWidth,
int nHeight
);
After you
call this
function
sucessfully,
it will
generate a
new bitmap
named
"temp.bmp"
under your
partation C
root folder.
Collapse
Copy
Code
SaveMemDCToBitmapNoPalete(HDC p_hMemDC,HBITMAP p_hBitmap)
{
HDC hMemDC = p_hMemDC;
HBITMAP hBitmap = p_hBitmap;
int wBitCount = 32;
BITMAP Bitmap;
BITMAPFILEHEADER bmfHdr;
BITMAPINFOHEADER bi;
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
HANDLE hDib = GlobalAlloc(GHND,dwBmBitsSize+sizeof(BITMAPINFOHEADER));
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
GetDIBits(hMemDC, hBitmap, 0, (UINT)Bitmap.bmHeight,
(LPSTR)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi, DIB_RGB_COLORS);
HANDLE fh = CreateFile("c:\\temp.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
ASSERT(fh != INVALID_HANDLE_VALUE);
DWORD dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
bmfHdr.bfType = 0x4D42;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
DWORD dwWritten;
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return TRUE;
}