DWORD size;
HDC hScrDC,hMemDC;
HBITMAP  hBitmap,hOldBitmap;
LPBITMAPINFOHEADER lpbi;
HANDLE hDib;         hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
         hBitmap = CreateCompatibleBitmap(hScrDC,800,600);
hMemDC = CreateCompatibleDC(hScrDC);
hOldBitmap = (HBITMAP)SelectObject(hMemDC,hBitmap);         size = 800*600*3;
         hDib  = GlobalAlloc(GHND,size+sizeof(BITMAPINFOHEADER));
         lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
         lpbi->biSize         = sizeof(BITMAPINFOHEADER);
         lpbi->biWidth        = 800;
         lpbi->biHeight       = 600;
         lpbi->biPlanes       = 1;         
         lpbi->biBitCount     = 24;
         lpbi->biCompression  = BI_RGB;
         lpbi->biSizeImage    = 0;
         lpbi->biXPelsPerMeter= 0;
         lpbi->biYPelsPerMeter= 0;
         lpbi->biClrUsed      = 0;
         lpbi->biClrImportant = 0;         while(TRUE){
  BitBlt(hMemDC,0,0,800,600,hScrDC,0,0,SRCCOPY);
  GetDIBits(hMemDC,
  hBitmap,
  0,
  0,
  (LPSTR)lpbi+sizeof(BITMAPINFOHEADER),
  (BITMAPINFO*)lpbi,DIB_RGB_COLORS
  );
  CDC* pDC=this->GetDC();
  SetDIBitsToDevice(pDC->GetSafeHdc(),0,0,100,100,0,0,0,100,
           (LPSTR)lpbi+sizeof(BITMAPINFOHEADER),
  (BITMAPINFO*)lpbi,DIB_RGB_COLORS
           );这里只是想显示出来看看结果,结果是一个黑色矩形
请帮忙找一下原因.

解决方案 »

  1.   

    下面的历程是如何得到整个屏幕的图像,得到某个窗口的图像的方法
    也是一样的;差别在于dc的选取。
    // Create a normal DC and a memory DC for the entire screen. The
    // normal DC provides a "snapshot" of the screen contents. The
    // memory DC keeps a copy of this "snapshot" in the associated
    // bitmap.hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
    hdcCompatible = CreateCompatibleDC(hdcScreen);// Create a compatible bitmap for hdcScreen.hbmScreen = CreateCompatibleBitmap(hdcScreen,
                         GetDeviceCaps(hdcScreen, HORZRES),
                         GetDeviceCaps(hdcScreen, VERTRES));if (hbmScreen == 0)
        errhandler("hbmScreen", hwnd);// Select the bitmaps into the compatible DC.if (!SelectObject(hdcCompatible, hbmScreen))
        errhandler("Compatible Bitmap Selection", hwnd); // Hide the application window.        ShowWindow(hwnd, SW_HIDE);         //Copy color data for the entire display into a
             //bitmap that is selected into a compatible DC.        if (!BitBlt(hdcCompatible,
                   0,0,
                   bmp.bmWidth, bmp.bmHeight,
                   hdcScreen,
                   0,0,
                   SRCCOPY))        errhandler("Screen to Compat Blt Failed", hwnd);        // Redraw the application window.        ShowWindow(hwnd, SW_SHOW);
      

  2.   

    呵呵,这样的截下的图就保存在hdcCompatible句柄所代表的memDC中了。
    你可以使用诸如
    CBitmap aBmp ;
    aBmp.Attach(...)或者直接::BitBlt(...)都可以。
      

  3.   

    See the sample below, FYI: http://www.codeproject.com/bitmap/screencapture.asp
      

  4.   

    sorry,可能没说清楚,我是要抓Dib,另外大概流程应该就是我那样,不过可能哪里出错了,所以我用SetDIBitsToDevice显示100*100图像出来验证一下,结果是黑色的正方形,所以想请大家帮忙找找问题所在。
      

  5.   

    问题找到了,GetDIBits的一个参数不小心写错了。不过还是谢谢serverclient和qing_li73(bluemoon)