如何将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给其他进程
我通过
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给其他进程
hImageBMP = LoadBitmap (lib,MAKEINTRESOURCE(pic_ID));
pic_ID就是里面的ID号,可以通过直接传ID号也可以实现你的想法,
GetBitmapBits, GetObject
通过这两个函数你可以把你想要的数据存到一个buffer里,
再用socket send给其他进程,之后将这个buffer还原成原始位图
我的代码: 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 返回错误!
这个函数该怎么用?