如何将HBITMAP转成char*
我通过
HWND hwnd = ::GetDesktopWindow();
HDC hsrc = ::GetDC(hwnd);
HDC hmemdc = ::CreateCompatibleDC(hsrc);
rc.NormalizeRect();
SIZE sz;
sz.cx=rc.right-rc.left;
sz.cy=rc.bottom-rc.top;
HBITMAP hbmp = ::CreateCompatibleBitmap(hsrc,sz.cx,sz.cy);
HGDIOBJ holdbmp = ::SelectObject(hmemdc,hbmp);
::BitBlt(hmemdc,0,0,sz.cx,sz.cx,hsrc,rc.left,rc.top,SRCCOPY);
::SelectObject(hmemdc,holdbmp);
::DeleteObject(hmemdc);
::ReleaseDC(hwnd,hsrc);获得了一幅图片
上面的代码执行完后,HBITMAP hbmp 应该指向这幅图
我想把hbmp中的内容写入一个字符串,用这个方法行吗?
char s[1000];
memcpy(s, hbmp, ??);
这样做主要是为把hbmp中的内容通过socket send给其他进程

解决方案 »

  1.   

    hbmp只不过是位图句柄,并不是实际的数据,你要用CBitmap.GetBitmapBits()获得数据,何必要要用Char,用byte不是更好
      

  2.   

    不是把HBITMAP转换成char*,可以把图片压到dll里面,
    hImageBMP = LoadBitmap (lib,MAKEINTRESOURCE(pic_ID));
    pic_ID就是里面的ID号,可以通过直接传ID号也可以实现你的想法,
      

  3.   

    给你两个函数用于通过hbitmap获得bits,和大小
    GetBitmapBits, GetObject
    通过这两个函数你可以把你想要的数据存到一个buffer里,
    再用socket send给其他进程,之后将这个buffer还原成原始位图
      

  4.   

    GetDIBits怎么用?
    我的代码: CRect rc;
    rc.top = 0;
    rc.bottom = GetSystemMetrics(SM_CYSCREEN);
    rc.left = 0;
     rc.right = GetSystemMetrics(SM_CXSCREEN);

    HWND hwnd = ::GetDesktopWindow();
    HDC hsrc = ::GetDC(hwnd);
    HDC hmemdc = ::CreateCompatibleDC(hsrc);
    rc.NormalizeRect();
    SIZE sz;
    sz.cx=rc.right-rc.left;
    sz.cy=rc.bottom-rc.top;
    HBITMAP hbmp = ::CreateCompatibleBitmap(hsrc,sz.cx,sz.cy);
    HGDIOBJ holdbmp = ::SelectObject(hmemdc,hbmp);
    ::BitBlt(hmemdc,0,0,sz.cx,sz.cx,hsrc,rc.left,rc.top,SRCCOPY);
    ::SelectObject(hmemdc,holdbmp);
    BITMAP mbmp;
    if (!GetObject(hbmp, sizeof(BITMAP), (LPSTR)&mbmp)) 
    {
    cout << "error 1" << endl;
    }; 
    int iBmpWidth = mbmp.bmWidth;
    int iBmpHeight = mbmp.bmHeight;
    int iBmpBitsPixel = mbmp.bmBitsPixel; int iSize = iBmpWidth*iBmpHeight*(iBmpBitsPixel/8);  byte* pData = new byte[iSize];BITMAPINFO bitMapInfo;memset(&bitMapInfo, 0, sizeof(bitMapInfo));bitMapInfo.bmiHeader.biSize   =   sizeof(BITMAPINFOHEADER);   int i_ret = GetDIBits(hDC,hbmp,0,(UINT)sz.cy ,pData,&bitMapInfo,DIB_RGB_COLORS);
    i_ret 返回错误!
      

  5.   

    GetDIBits
    这个函数该怎么用?