1

我正在使用此功能从特定窗口或整个桌面截取屏幕截图,它之所以有效,是因为我看到了粘贴到 MSPaint 的结果。但我不想将结果写入一个*.bmp或任何类型的文件,我想通过将此位图数据发送到远程服务器来从头开始制作一个简单的 vnc 客户端。我可以直接通过套接字将此位图发送到服务器吗?或者我应该将其转换为bytestring首先。

static void MyPrintWindow(HWND hWnd)
    {
        RECT rc;
        GetWindowRect(hWnd, &rc);
        const HDC hScreenDC = GetDC(nullptr);
        const HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
        const int width = GetDeviceCaps(hScreenDC, HORZRES);
        const int height = GetDeviceCaps(hScreenDC, VERTRES);
        hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
        HBITMAP(SelectObject(hMemoryDC, hBitmap));
        BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);

        PrintWindow(hWnd, hMemoryDC, PW_CLIENTONLY);

        OpenClipboard(nullptr);
        EmptyClipboard();
        SetClipboardData(CF_BITMAP, hBitmap);
        CloseClipboard();

        DeleteDC(hMemoryDC);
        DeleteDC(hScreenDC);
    }

这是我尝试的HBITMAP转换Byte函数,它返回 emtpy 字节:

static byte BitmapToByte(HBITMAP bit)
{
    BITMAP bitmap;
    GetObject(bit, sizeof(BITMAP), &bitmap);
    const int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel / 8;
    byte lp_bits;
    GetBitmapBits(HBITMAP(bit), size, &lp_bits);

    return lp_bits;
}

我也阅读了一些开源应用程序,他们使用这种方法,但是转换部分太复杂了。

更新: 所以我发现了这个:c++ how to send Hbitmap over socket,它接近我想要做的但解决方案不完整,但是我明白了,只需从位图中获取像素然后发送这些像素或保存那些像素数据到字节数组,然后发送数据。

4

0 回答 0