使用CreateDIBSection 可以创建一个设备无关位图
#include <windows.h> using namespace std; int main() { HDC hdc = GetDC(HWND_DESKTOP); HDC MemDC = CreateCompatibleDC(hdc); // HBITMAP hBit = CreateCompatibleBitmap(hdc, 1366, 768); BITMAPINFO bmi; memset(&bmi, 0, sizeof(BITMAPINFO)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = 1366; bmi.bmiHeader.biHeight = 768; // top-down bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; PVOID m_pBits; HBITMAP m_hBmp = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&m_pBits, NULL, NULL); SelectObject(MemDC, m_hBmp); BitBlt(MemDC, 0, 0, 1366, 768, hdc, 0, 0, SRCCOPY); BitBlt(hdc, 0, 0, 1366, 768, MemDC, 0, 0, SRCCOPY); // DeleteObject(hBit); DeleteObject(m_hBmp); ReleaseDC(HWND_DESKTOP, hdc); ReleaseDC(NULL, MemDC); DeleteDC(MemDC); DeleteDC(hdc); }
顺便说一下,当使用CreateCompatibleBitmap以内存dc(MemDC )为对象时,会创建单色位图。