簡體   English   中英

c ++如何通過套接字發送Hbitmap

[英]c++ how to send Hbitmap over socket

我的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!?! 請幫我。 謝謝。

獲得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);
}

始終發送位圖的像素數據(無符號字符 *)。 HBITMAP 只是位圖的句柄。 還要注意尺寸問題,如果你的高度、寬度和 bpp 很高,你需要考慮壓縮像素數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM