怎么样获得句柄HBITMAP的位图数据流信息;代码如下:CRect Rect;
::GetWindowRect(hwnd,Rect);
HDC hSourceDC = ::GetDC(hwnd);
HDC hdc = ::CreateCompatibleDC(hSourceDC);HBITMAP hCaptureBitmap = ::CreateCompatibleBitmap(hSourceDC, Rect.Width(), Rect.Height());
::BitBlt(hdc,0,0,Rect.Width(),Rect.Height(),hSourceDC,0,0,SRCCOPY); 怎么样获得hCaptureBitmap的位图数据流呢
我采用
BITMAP bm;
::GetObject(hCaptureBitmap,sizeof(bm),(LPVOID)&bm);
可是bm的bmBits为NULL
各位有什么办法嘛

解决方案 »

  1.   

    先把HBITMAP用CBitmap::FromHandle转为CBitmap*,再用CBitmap::GetBitmapBits获取。
      

  2.   

    GetBitmapBits会进行一次内存拷贝,可以避免马
      

  3.   

    int GetObject(
      HGDIOBJ hgdiobj,  // handle to graphics object
      int cbBuffer,     // size of buffer for object information
      LPVOID lpvObject  // buffer for object information
    );
    If hgdiobj is a handle to a bitmap created by calling CreateDIBSection, and the specified buffer is large enough, the GetObject function returns a DIBSECTION structure. In addition, the bmBits member of the BITMAP structure contained within the DIBSECTION will contain a pointer to the bitmap's bit values. If hgdiobj is a handle to a bitmap created by any other means, GetObject returns only the width, height, and color format information of the bitmap. You can obtain the bitmap's bit values by calling the GetDIBits or GetBitmapBits function.
      

  4.   

    不想Copy内存中数据的话,就按一楼WecanHuang(曾阿牛)朋友的方法,bmp文件格式看这里:
    http://community.csdn.net/Expert/topic/4614/4614713.xml?temp=9.312075E-02
    希望能帮到你!
      

  5.   

    BOOL SaveBitmap(HDC hDC,HBITMAP hBitmap,CString szPath)
    {
        FILE * fp= NULL;
        fp = fopen(szPath.GetBuffer(1),"wb");
        if(fp == NULL)
            return false;
        
        BITMAP Bm;
        BITMAPINFO BitInfo;
        ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
        BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        BitInfo.bmiHeader.biBitCount = 0;    if(!::GetDIBits(hDC, hBitmap, 0, 0, NULL, &BitInfo, DIB_RGB_COLORS))
            return (false);    Bm.bmHeight = BitInfo.bmiHeader.biHeight;
        Bm.bmWidth  = BitInfo.bmiHeader.biWidth;    BITMAPFILEHEADER    BmHdr;
        
        BmHdr.bfType        = 0x4d42;   // 'BM' WINDOWS_BITMAP_SIGNATURE
        BmHdr.bfSize        = (((3 * Bm.bmWidth + 3) & ~3) * Bm.bmHeight) 
                              + sizeof(BITMAPFILEHEADER) 
                              + sizeof(BITMAPINFOHEADER);
        BmHdr.bfReserved1    = BmHdr.bfReserved2 = 0;
        BmHdr.bfOffBits      = (DWORD) sizeof(BITMAPFILEHEADER) 
                              + sizeof(BITMAPINFOHEADER);
        
        BitInfo.bmiHeader.biCompression = 0;
        // Writing Bitmap File Header ////
        fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,fp);    fwrite(&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);    BYTE *pData = new BYTE[BitInfo.bmiHeader.biSizeImage + 5];
        if(!::GetDIBits(hDC, hBitmap, 0, Bm.bmHeight, 
                     pData, &BitInfo, DIB_RGB_COLORS))
            return (false);
        if(pData != NULL)
            fwrite(pData,1,BitInfo.bmiHeader.biSizeImage,fp);    fclose(fp);
        delete (pData);    return (true);
    }
      

  6.   

    兄弟我综合你们的, 搞定了.谢谢
    CFileDialog dlg(true,_T("BMP"),_T("bmp"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.*|*.*|")); if (dlg.DoModal()!=IDOK)
    return;
    CString ss=dlg.GetPathName();
    //m_bmp.LoadBitmap(ss);
    hBmp=(HBITMAP)LoadImage(AfxGetInstanceHandle(),ss,
                                IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

        CBitmap *pBmp=CBitmap::FromHandle(hBmp);
        BITMAP bm;
        ::GetObject(hBmp,sizeof(bm),(LPVOID)&bm);
    m_bmWidth=bm.bmWidth;
    m_bmHeight=bm.bmHeight;