求printwindow函数获取图片后无法保存到缓冲区的解决办法

求printwindow函数获取图片后无法保存到缓冲区的解决方法
本帖最后由 xiaohei1119 于 2014-09-16 16:49:06 编辑
                HDC hScrDC, hMemDC;  
                CRect  m_WindowRect;
::GetWindowRect(m_Winhwnd , m_WindowRect);
hScrDC = ::GetDC(m_Winhwnd);
ASSERT(hScrDC);
hMemDC = ::CreateCompatibleDC(hScrDC);
ASSERT(hMemDC);

HBITMAP hBitmap,hOldBitmap; 
hBitmap=::CreateCompatibleBitmap(hScrDC, m_WindowRect.Width(), m_WindowRect.Height());
ASSERT(hBitmap);
hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);

unsigned char *lpvBits = NULL;
  LONG ImageSize = m_WindowRect.Width()*m_WindowRect.Height()*4 ;
lpvBits = new unsigned char[ImageSize];

BITMAPINFO bmpInfo;
memset(&bmpInfo, 0 , sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader);
bmpInfo.bmiHeader.biWidth=m_WindowRect.Width(); 
bmpInfo.bmiHeader.biHeight =m_WindowRect.Height(); 
bmpInfo.bmiHeader.biPlanes = 1; 
bmpInfo.bmiHeader.biBitCount = 32; 
bmpInfo.bmiHeader.biCompression = BI_RGB; 
bmpInfo.bmiHeader.biSizeImage = ImageSize;

::PrintWindow(m_Winhwnd , hMemDC , 0);
OpenClipboard(NULL);
EmptyClipboard(); 
SetClipboardData(CF_BITMAP, hBitmap); 
CloseClipboard();                                             //剪切板验证图片是正确拿到的
::GetDIBits(hMemDC, hBitmap, 0 , m_WindowRect.Height() , lpvBits , &bmpInfo,  DIB_RGB_COLORS);
                //这里的返回值也是图片高度,只是lpvBits值为0
hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap);
DeleteDC(hMemDC);
DeleteDC(hScrDC);
DeleteObject(hBitmap);



最近在做一个桌面取图的程序,这个是对窗口进行取图,取图后会把图片保存在缓存区,然后进一步操作。我桌面取图和桌面区域取图都没问题,就这个的指针老是为零。
------解决思路----------------------
仅供参考
#pragma comment(lib,"gdi32")
#include <windows.h>
#include <stdio.h>

int main() {
    const DWORD uWidth = 18 + 17 * 256, uHeight = 18 + 17 * 128;

    PBITMAPINFO pbmi = (PBITMAPINFO) LocalAlloc (LPTR, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2);
    pbmi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
    pbmi->bmiHeader.biWidth = uWidth;
    pbmi->bmiHeader.biHeight = uHeight;
    pbmi->bmiHeader.biPlanes = 1;
    pbmi->bmiHeader.biBitCount = 1;
    pbmi->bmiHeader.biSizeImage = ((uWidth + 31) & ~31) / 8 * uHeight;
    pbmi->bmiColors[0].rgbBlue = 0;
    pbmi->bmiColors[0].rgbGreen = 0;
    pbmi->bmiColors[0].rgbRed = 0;
    pbmi->bmiColors[1].rgbBlue = 255;
    pbmi->bmiColors[1].rgbGreen = 255;
    pbmi->bmiColors[1].rgbRed = 255;

    HDC hDC = CreateCompatibleDC (0);
    void * pvBits;
    HBITMAP hBitmap = CreateDIBSection (hDC, pbmi, 0, &pvBits, NULL, 0);
    SelectObject (hDC, hBitmap);
    HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
//  HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, "宋体");
    SelectObject (hDC, hFont);
    BitBlt (hDC, 0, 0, uWidth, uHeight, NULL, 0, 0, WHITENESS);

    char c[4];
    int i, j;
    for (i = 128; i < 256; i++) {
        sprintf (c, "%02X", i);
        TextOut (hDC, 1, (i - 127) * 17 + 1, c, 2);
    }
    for (j = 0; j < 256; j++) {
        sprintf (c, "%02X", j);
        TextOut (hDC, (j + 1)* 17 + 1, 1, c, 2);
    }
    for (i = 128; i < 256; i++) {