代码如下: // TODO: Add extra validation here
HBITMAP hOldBitmap, hbmScreen; HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL); 
HDC hdcCompatible = CreateCompatibleDC(hdcScreen); 

// Create a compatible bitmap for hdcScreen. 

hbmScreen = CreateCompatibleBitmap(hdcScreen, 
GetDeviceCaps(hdcScreen, HORZRES), 
GetDeviceCaps(hdcScreen, VERTRES)); 

if (hbmScreen == 0) 
{
AfxMessageBox("hbmScreen"); 
return;
}

// Select the bitmaps into the compatible DC. 
hOldBitmap  =(HBITMAP)SelectObject(hdcCompatible, hbmScreen); if (!hOldBitmap) 
{
AfxMessageBox("Compatible Bitmap Selection"); 
return;
}

//Copy color data for the entire display into a 
//bitmap that is selected into a compatible DC. 

if (!BitBlt(hdcCompatible, 
0,0, 
GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES), 
hdcScreen, 
0,0, 
SRCCOPY)) 
{
AfxMessageBox("Screen to Compat Blt Failed"); 
return;
} hbmScreen = (HBITMAP) SelectObject(hdcCompatible, hOldBitmap);
//==================================================================

    BITMAP bmp; 
    PBITMAPINFO pbmi; 
    WORD    BitsPerPixel; 

    // Retrieve the bitmap color format, width, and height. 
//根据位图句柄获取位图信息
    if (!GetObject(hbmScreen, sizeof(BITMAP), (LPSTR)&bmp)) 
{
AfxMessageBox("GetObject"); 
return;
}

    // Convert the color format to a count of bits. 
    BitsPerPixel = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
    if (BitsPerPixel == 1) 
        BitsPerPixel = 1; 
    else if (BitsPerPixel <= 4) 
        BitsPerPixel = 4; 
    else if (BitsPerPixel <= 8) 
        BitsPerPixel = 8; 
    else if (BitsPerPixel <= 16) 
        BitsPerPixel = 16; 
    else if (BitsPerPixel <= 24) 
        BitsPerPixel = 24; 
    else BitsPerPixel = 32;     // Allocate memory for the BITMAPINFO structure. (This structure 
    // contains a BITMAPINFOHEADER structure and an array of RGBQUAD 
    // data structures.) 

if (BitsPerPixel != 24) 
pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
sizeof(BITMAPINFOHEADER) + 
sizeof(RGBQUAD) * (1<< BitsPerPixel)); 

// There is no RGBQUAD array for the 24-bit-per-pixel format. 

else 
pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
sizeof(BITMAPINFOHEADER)); 

    // Initialize the fields in the BITMAPINFO structure. 

    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    pbmi->bmiHeader.biWidth = bmp.bmWidth; 
    pbmi->bmiHeader.biHeight = bmp.bmHeight; 
    pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
    if (BitsPerPixel < 24) 
        pbmi->bmiHeader.biClrUsed = (1<<BitsPerPixel); 

    // If the bitmap is not compressed, set the BI_RGB flag. 
    pbmi->bmiHeader.biCompression = BI_RGB; 

    // Compute the number of bytes in the array of color 
    // indices and store the result in biSizeImage. 
    // For Windows NT, the width must be DWORD aligned unless 
    // the bitmap is RLE compressed. This example shows this. 
    // For Windows 95/98/Me, the width must be WORD aligned unless the 
    // bitmap is RLE compressed.
    pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * BitsPerPixel +31) & ~31) /8 * pbmi->bmiHeader.biHeight; 
    // Set biClrImportant to 0, indicating that all of the 
    // device colors are important. 
pbmi->bmiHeader.biClrImportant = 0; 

//===================================================================
PBITMAPINFOHEADER pbih = (PBITMAPINFOHEADER) pbmi; LPBYTE lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
    if (!GetDIBits(hdcCompatible, hbmScreen, 0, pbih->biHeight, lpBits, pbmi, 
        DIB_RGB_COLORS)) 
    {
        AfxMessageBox("GetDIBits");
return;
    } DWORD ErrCode = ::GetLastError();
if (ErrCode != 0)
{
CString ErrString;
ErrString.Format("%d", ErrCode);
AfxMessageBox(ErrString);
}

HBITMAP hBitmap = ::CreateBitmap(pbih->biWidth, pbih->biHeight, pbih->biPlanes, pbih->biBitCount, lpBits); 
CDC *pDC = GetDC();
pDC->DrawState(CPoint(0,0), CSize(400,300), hBitmap, DST_BITMAP); ReleaseDC(pDC);
DeleteDC(hdcScreen);
DeleteDC(hdcCompatible); ::DeleteObject(hOldBitmap);
::DeleteObject(hbmScreen);
::DeleteObject(hBitmap);
GlobalFree((HGLOBAL)lpBits);

解决方案 »

  1.   

    不想多看了,就想问一下,你是在bitmap里一个点一个点读出来显示的吗?如果是的话,问题可能在于,其实位图里面的信息是从下往上存的,就是说最后一行的信息存在最前面,第一行存在最后面。
      

  2.   

    TO EddieCai() 那怎样才能反转过来啊
      

  3.   

    pbmi->bmiHeader.biHeight = bmp.bmHeight; 
    改为pbmi->bmiHeader.biHeight = -bmp.bmHeight; 就可以了