16,548
社区成员




HDC hDC = ::GetDC(NULL);
HBITMAP hMainSurface = ::CreateCompatibleBitmap(hDC, 800, 600); // 主画布
::ReleaseDC(NULL, hDC);
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
HDC hMainDC = ::GetDC(hWnd);
// 绘制部分
{
RECT rcRect = { 0, 0, 800, 600 };
FillRect(hMainDC, &rcRect, CreateSolidBrush(RGB(255, 255, 255)));
(HBITMAP)::SelectObject(hMainDC, hMainSurface);
TextOutA(hMainDC, 10, 10, "test", strlen("test"));
// 请问在这里如何获取当前hMainDC中的RGB数据?我想读出来,以进行混合运算
// 另一方面,此时的主画布hMainSurface中有RGB像素么?我用GetBitmapBits()获取好像什么都没有。
}
::ReleaseDC(hWnd, hMainDC);
}
// 保存窗口图像为tga文件
void PrintScreen(HDC hSrcDC, HBITMAP hBitmap, const char* szFile)
{
BYTE* pBmp = (BYTE*)malloc(800*600*3);
UINT* pBmp32 = (UINT*)malloc(800*600*sizeof(UINT));
{
HDC hMemoryDC = CreateCompatibleDC(NULL);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
StretchBlt(hMemoryDC, 0, 0, 800, 600, hSrcDC, 0, 0, 800, 600,SRCCOPY);
BITMAPINFO bmpInfo;
memset(&bmpInfo, 0, sizeof(bmpInfo));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = 800;
bmpInfo.bmiHeader.biHeight = 600;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biBitCount = 24;
GetDIBits(hMemoryDC, hBitmap, 0, 600, pBmp, &bmpInfo, DIB_RGB_COLORS);
if (hOldBitmap != NULL)
{
::SelectObject(hMemoryDC, hOldBitmap);
}
}
BYTE* pSrc = pBmp;
#define CZGDI_RGB(r, g, b) ((r<<16) | (g<<8) | b)
for (int h=0; h<600; h++)
{
for (int w=0; w<800; w++)
{
BYTE r = *pSrc++;
BYTE g = *pSrc++;
BYTE b = *pSrc++;
pBmp32[h*800+w] = CZGDI_RGB(r, g, b) | 0xff000000;
}
}
// 保存
SaveTgaFile(szFile, pBmp32, 800, 600);
free(pBmp32);
free(pBmp);
}
其中,SaveTgaFile()函数是自己写的,功能是将32位的bmp数据写成tga文件。
HDC hMemDC = CreateCompatibleDC(NULL);
HDC hDC = ::GetDC(NULL);
HBITMAP hMainSurface = ::CreateCompatibleBitmap(hDC, 800, 600);
::ReleaseDC(NULL, hDC);
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
HDC hMainDC = ::GetDC(hWnd);
// 绘制部分
{
RECT rcRect = { 0, 0, 800, 600 };
FillRect(hMainDC, &rcRect, CreateSolidBrush(RGB(255, 255, 255)));
(HBITMAP)::SelectObject(hMainDC, hMainSurface);
TextOutA(hMainDC, 10, 10, "test", strlen("test"));
if (GetKeyState(VK_F1) & 0x8000)
{
BYTE* pBmp = (BYTE*)malloc(800*600*3);
{
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hMainSurface);
StretchBlt(hMemDC, 0, 0, 800, 600, hMainDC, 0, 0, 800, 600,SRCCOPY);
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = 800;
bmpInfo.bmiHeader.biHeight = 600;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biBitCount = 24;
GetDIBits(hMemDC, hMainSurface, 0, 600, pBmp, &bmpInfo, DIB_RGB_COLORS);
}
// 保存
SaveTgaFile("out.tga", pBmp, 800, 600);
free(pBmp32);
free(pBmp);
}
}
::ReleaseDC(hWnd, hMainDC);
Sleep(60);
}