【问题标题】:c++ how to send Hbitmap over socketc ++如何通过套接字发送Hbitmap
【发布时间】:2014-04-29 15:38:58
【问题描述】:

我的GetScreen 函数看起来像:

void GetScreen(int clientSocket, const char *filename) {   
         HDC hDC = NULL;
         int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
         int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
         HWND hDesktopWnd = GetDesktopWindow();
         HDC hDesktopDC = GetDC(hDesktopWnd);
         HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
         HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC, 
                                 nScreenWidth, nScreenHeight);
         SelectObject(hCaptureDC,hCaptureBitmap); 
         BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
                hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); 
         SaveBitmap(clientSocket, "test.bmp",hCaptureBitmap); //here to save the captured image to disk
         **//here to send Hbitmap: send(clientSocket,?,?,Null);**

         ReleaseDC(hDesktopWnd,hDesktopDC);
         DeleteDC(hCaptureDC);
         DeleteObject(hCaptureBitmap);
}

现在我想通过套接字发送HBITMAP 而不保存位图。 我用谷歌搜索并找到了GetDIBitsSetDIBits 但我不知道如何准确地使用它。有人能帮我吗?谢谢。

现在我尝试使用以下代码从 HBITMAP 获取 BYTE:

BYTE* getPixArray(HBITMAP hBitmap)
        {
        HDC hdc,hdcMem;

        hdc = GetDC(NULL);
        hdcMem = CreateCompatibleDC(hdc); 

        BITMAPINFO MyBMInfo = {0};
        MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
        // Get the BITMAPINFO structure from the bitmap
        if(0 == GetDIBits(hdcMem, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS))
        {
            cout<<"FAIL\n"<<endl;
        }

        // create the bitmap buffer
        BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];

        MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
        MyBMInfo.bmiHeader.biBitCount = 32;  
        MyBMInfo.bmiHeader.biCompression = BI_RGB;  
        MyBMInfo.bmiHeader.biHeight = (MyBMInfo.bmiHeader.biHeight < 0) ? (-MyBMInfo.bmiHeader.biHeight) : (MyBMInfo.bmiHeader.biHeight); 

        // get the actual bitmap buffer
        if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS))
        {
            cout<<"FAIL\n"<<endl;
        }

        return lpPixels;
    }

现在在套接字的另一端,我想从 BYTE 获取 HBITMAP!?! 请帮我。谢谢。

【问题讨论】:

标签: c++ windows gdi


【解决方案1】:

收到HBITMAP 后,请致电HBITMAPToPixels。发送widthheightbitsperpixel。接下来发送像素..

在套接字的另一端,读取width,读取height,读取bitsperpixel。 然后创建一个大小为:((width * Bmp.bmBitsPixel + 31) / 32) * 4 * height 的缓冲区 将这么多字节读入缓冲区并调用HBITMAPFromPixels

讨论的函数定义如下..

#include <iostream>
#include <stdexcept>
#include <vector>
#include <cstring>
#include <memory>
#include <windows.h>

std::unique_ptr<std::remove_pointer<HBITMAP>::type, std::function<void(HBITMAP)>> HBITMAPFromPixels(const std::vector<std::uint8_t> &Pixels, std::uint32_t width, std::uint32_t height, std::uint16_t BitsPerPixel)
{
    BITMAPINFO Info = {0};
    std::memset(&Info, 0, sizeof(BITMAPINFO));

    Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    Info.bmiHeader.biWidth = width;
    Info.bmiHeader.biHeight = -height;
    Info.bmiHeader.biPlanes = 1;
    Info.bmiHeader.biBitCount = BitsPerPixel;
    Info.bmiHeader.biCompression = BI_RGB;
    Info.bmiHeader.biSizeImage = ((width * BitsPerPixel + 31) / 32) * 4 * height;

    HBITMAP Result = CreateDIBitmap(GetDC(nullptr), &Info.bmiHeader, CBM_INIT, &Pixels[0], &Info, DIB_RGB_COLORS);
    return std::unique_ptr<std::remove_pointer<HBITMAP>::type, std::function<void(HBITMAP)>>(Result, [&](HBITMAP hBmp){DeleteObject(hBmp);});
}

void HBITMAPToPixels(HBITMAP BitmapHandle, std::vector<std::uint8_t> &Pixels, std::uint32_t &width, std::uint32_t &height, std::uint16_t &BitsPerPixel)
{
    if (BitmapHandle == nullptr)
    {
        throw std::logic_error("Null Pointer Exception. BitmapHandle is Null.");
    }

    Pixels.clear();
    BITMAP Bmp = {0};
    BITMAPINFO Info = {0};
    HDC DC = CreateCompatibleDC(nullptr);
    std::memset(&Info, 0, sizeof(BITMAPINFO));
    HBITMAP OldBitmap = (HBITMAP)SelectObject(DC, BitmapHandle);
    GetObject(BitmapHandle, sizeof(Bmp), &Bmp);

    Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    Info.bmiHeader.biWidth = width = Bmp.bmWidth;
    Info.bmiHeader.biHeight = height = Bmp.bmHeight;
    Info.bmiHeader.biPlanes = 1;
    Info.bmiHeader.biBitCount = BitsPerPixel = Bmp.bmBitsPixel;
    Info.bmiHeader.biCompression = BI_RGB;
    Info.bmiHeader.biSizeImage = ((width * Bmp.bmBitsPixel + 31) / 32) * 4 * height;

    Pixels.resize(Info.bmiHeader.biSizeImage);
    GetDIBits(DC, BitmapHandle, 0, height, &Pixels[0], &Info, DIB_RGB_COLORS);
    SelectObject(DC, OldBitmap);
    height = height < 0 ? -height : height;
    DeleteDC(DC);
}

【讨论】:

  • 有很多未定义的东西,你忘了一些包含吗?
  • #include
【解决方案2】:

始终发送位图的像素数据(无符号字符 *)。 HBITMAP 只是位图的句柄。还要注意尺寸问题,如果你的高度、宽度和 bpp 很高,你需要考虑压缩像素数据。

【讨论】:

    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 2020-03-19
    • 2013-09-02
    • 2012-01-20
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    相关资源
    最近更新 更多