gpt4 book ai didi

c++ - 通过套接字发送 HBITMAP 会出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:06 24 4
gpt4 key购买 nike

所以我尝试通过套接字发送 HBITMAP,并遇到了一些问题。我知道 HBITMAP 是一个句柄,一个指向图像字节的内存引用指针。所以我所做的是在谷歌上查找如何将其转换为字节数组,从而能够发送它:

void *pktdata;
std::vector<uint8_t> pixels;

uint32_t width;
uint32_t height;
uint16_t BitsPerPixel;

HBITMAPToPixels(texture, pixels, width, height, BitsPerPixel);

int pktsize = sizeof(PKT_Structure_s) + sizeof(std::vector<uint8_t>) + (sizeof(uint8_t) * pixels.size());

PKT_Structure_s n;
n.width = width;
n.height = height;
n.BitsPerPixel = BitsPerPixel;
n.DataSize = sizeof(std::vector<uint8_t>) + (sizeof(uint8_t) * pixels.size());

memcpy(&pktdata, &n, sizeof(n));
memcpy(&pktdata + sizeof(n), &pixels, n.DataSize);

p2pSendToHost(NULL, (DefaultPacket*)pktdata, pktsize);
delete[] pktdata;
return;

这是我从另一个 stackoverflor 问题得到的 HBITMAPToPixels:

void HBITMAPToPixels(HBITMAP BitmapHandle, std::vector<uint8_t> &Pixels, uint32_t &width, uint32_t &height, uint16_t &BitsPerPixel) 
{

if (BitmapHandle == NULL)
{
throw std::logic_error("Null Pointer Exception. BitmapHandle is Null.");
}

Pixels.clear();
BITMAP Bmp = { 0 };
BITMAPINFO Info = { 0 };
HDC DC = CreateCompatibleDC(NULL);
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);

}

这有时会崩溃,有时它实际上发送了数据包,但服务器随后崩溃了。这是服务器处理数据包的方式:

void Server::ProcessScreenShot(const void * packetdatadata, int packetsizesize)
{
PKT_Structure_sn;
std::vector<uint8_t> pixels;
int pktsize = sizeof(PKT_Structure_s) + sizeof(std::vector<uint8_t>) + (sizeof(uint8_t) * pixels.size());

memcpy(&n, packetdatadata, sizeof(n)); //Get Our Packet Struct
memcpy(&pixels, &packetdatadata + sizeof(n), n.DataSize); // Get Our Image in bytes

HBITMAP btmp = HBITMAPFromPixels(pixels, n.width, n.height, n.BitsPerPixel);

time_t t = time(0);
struct tm * now = localtime(&t);

char filename[MAX_PATH];
sprintf(filename, "SS.bmp");

HPALETTE hpal = NULL;
saveBitmap(filename, btmp, hpal);

return;
}

最后但同样重要的是,我在问题答案中使用的另一个函数来取回我的 HBITMAP:

HBITMAP HBITMAPFromPixels(const std::vector<uint8_t> &Pixels, uint32_t width, uint32_t height, 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(NULL), &Info.bmiHeader, CBM_INIT, &Pixels[0], &Info, DIB_RGB_COLORS);
return Result;
}

保存功能无关紧要,因为我已经在控制台应用程序上快速测试过它并且可以正常工作。

我认为我可能在 memcpy 上做错了,因为我在做那件事时真的很困惑,这很可能会崩溃。

谢谢

最佳答案

问题不是初始化 pkdata 和传递 vector 错误,你必须传递它 &vec[0]

关于c++ - 通过套接字发送 HBITMAP 会出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29192583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com