Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过套接字发送HBITMAP给出问题_C++_Sockets_Bitmap - Fatal编程技术网

C++ 通过套接字发送HBITMAP给出问题

C++ 通过套接字发送HBITMAP给出问题,c++,sockets,bitmap,C++,Sockets,Bitmap,所以我试图通过套接字发送一个HBITMAP,并讨论一些问题。 我知道HBITMAP是一个句柄,一个指向图像字节的内存引用指针。 我所做的就是在google上查找如何将其转换为字节数组,从而能够发送它: void *pktdata; std::vector<uint8_t> pixels; uint32_t width; uint32_t height; uint16_t BitsPerPixel; HBITMAPToPixels(texture, pixels, width, h

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

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; 
void*pktdata;
std::矢量像素;
uint32_t宽度;
uint32_t高度;
uint16_t比特像素;
HBITMAPToPixels(纹理、像素、宽度、高度、位像素);
int-pktsize=sizeof(PKT_结构)+sizeof(std::vector)+(sizeof(uint8_t)*pixels.size();
PKT_结构;
n、 宽度=宽度;
n、 高度=高度;
n、 BitsPerPixel=BitsPerPixel;
n、 DataSize=sizeof(std::vector)+(sizeof(uint8_t)*pixels.size());
memcpy(&pktdata,&n,sizeof(n));
memcpy(&pktdata+sizeof(n),&pixels,n.DataSize);
p2pSendToHost(NULL,(DefaultPacket*)pktdata,pktsize);
删除[]pktdata;
回来
这是我从StackOverver的另一个问题中得到的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 hbitmaptopixel(HBITMAP位图句柄,标准::矢量和像素,uint32_t和宽度,uint32_t和高度,uint16_t和位像素)
{
if(BitmapHandle==NULL)
{
抛出std::logic_错误(“空指针异常。BitmapHandle为空”);
}
像素。清除();
位图Bmp={0};
BITMAPINFO信息={0};
HDC DC=CreateCompatibleDC(空);
std::memset(&Info,0,sizeof(BITMAPINFO));
HBITMAP OldBitmap=(HBITMAP)选择对象(DC,位图句柄);
GetObject(位图句柄、sizeof(Bmp)和&Bmp);
Info.bmiHeader.biSize=sizeof(BitMapInfo头文件);
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=((宽度*Bmp.bmBitsPixel+31)/32)*4*高度;
像素。调整大小(Info.bmiHeader.biSizeImage);
GetDIBits(DC、位图句柄、0、高度和像素[0]、&Info、DIB_RGB_颜色);
选择对象(DC、旧位图);
高度=高度<0?-高度:高度;
删除DC(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;
}
void服务器::进程屏幕截图(const void*packetdatadata,int packetsizesize)
{
PKT_结构;
std::矢量像素;
int-pktsize=sizeof(PKT_结构)+sizeof(std::vector)+(sizeof(uint8_t)*pixels.size();
memcpy(&n,packetdatadata,sizeof(n));//获取数据包结构
memcpy(&pixels,&packetdatadata+sizeof(n),n.DataSize);//以字节为单位获取图像
HBITMAP btmp=HBITMAPFromPixels(像素,n.宽度,n.高度,n.位像素);
时间t=时间(0);
struct tm*now=localtime(&t);
字符文件名[最大路径];
sprintf(文件名,“SS.bmp”);
HPALETTE hpal=NULL;
保存位图(文件名、btmp、hpal);
回来
}
最后但并非最不重要的一点是,我从问题答案中使用了另一个函数来获取我的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;
}
HBITMAP HBITMAPFromPixels(const std::vector&Pixels,uint32_t width,uint32_t height,uint16_t BitsPerPixel)
{
BITMAPINFO信息={0};
std::memset(&Info,0,sizeof(BITMAPINFO));
Info.bmiHeader.biSize=sizeof(BitMapInfo头文件);
Info.bmiHeader.biWidth=宽度;
Info.bmiHeader.biHeight=-高度;
Info.bmiHeader.biPlanes=1;
Info.bmiHeader.biBitCount=BitsPerPixel;
Info.bmiHeader.biCompression=BI_RGB;
Info.bmiHeader.biSizeImage=((宽度*比特像素+31)/32)*4*高度;
HBITMAP Result=CreateDIBitmap(GetDC(NULL),&Info.bmiHeader,CBM_INIT,&Pixels[0],&Info,DIB_RGB_COLORS);
返回结果;
}
保存功能是不相关的,因为我在一个控制台应用程序上测试了它,速度非常快,而且可以正常工作

我想memcpy可能做错了,因为我在做这件事的时候真的很困惑,这是个问题


谢谢

问题不是初始化pkdata和错误地传递向量,你必须像这样传递它
&vec[0]

在调试器中运行它,缩小崩溃的范围。是的,这就是问题所在。我无法调试它,因为它是在服务器上编译的,很可能甚至无法运行游戏,我尝试了remotedebug,但从未让它工作。如果将它转换为字节,则根本不通过套接字发送HBITMAP,而是发送字节。所以你的标题错了。或者至少你正试着这么做。您到底在哪里分配
pktdata
?提示:您没有。在调试时,没有任何东西可以阻止您在开发计算机上同时运行服务器和客户端。是的,服务器的图形卡不好,因此在其中运行游戏会冻结整个过程。关于pktdata,分配是什么意思?