在套接字上发送HBITMAP会产生问题

时间:2015-03-22 08:58:51

标签: c++ sockets bitmap

所以我试图通过套接字发送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而做错了,因为我在做这件事时感到非常困惑,这是probs崩溃。

由于

1 个答案:

答案 0 :(得分:0)

问题不在于初始化pkdata并且错误地传递了向量,你必须像传递它一样      &安培; VEC [0]