gpt4 book ai didi

c++ - 从字符数组中检索图像

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:19 25 4
gpt4 key购买 nike

我已经从答案帖子 here 中改编了这段代码的一部分它将屏幕截图 jpg 图像保存到缓冲区中。我正在使用 sento() 通过 UDP 发送此缓冲图像。我无法将发送回图像的此字符数组数据转换为图像。感谢您提供的任何帮助

void gdiscreen()
{
char buffer[61400];
using namespace Gdiplus;
wchar_t filename[200];
memset(filename,0,sizeof(filename));
cnt++;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

EncoderParameters encoderParameters;
ULONG quality;
{

HDC scrdc, memdc;
HBITMAP membit;
scrdc = ::GetDC(0);
int Height = GetSystemMetrics(SM_CYSCREEN);
int Width = GetSystemMetrics(SM_CXSCREEN);
memdc = CreateCompatibleDC(scrdc);
membit = CreateCompatibleBitmap(scrdc, Width, Height);
HBITMAP hOldBitmap =(HBITMAP) SelectObject(memdc, membit);
BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
Gdiplus::Bitmap bitmap(membit, NULL);
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);


encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
quality = 20;
encoderParameters.Parameter[0].Value = &quality;

IStream *pStream = NULL;
LARGE_INTEGER liZero = {};
ULARGE_INTEGER pos = {};
STATSTG stg = {};
ULONG bytesRead=0;
HRESULT hrRet=S_OK;
// this is your buffer that will hold the jpeg bytes
DWORD dwBufferSize = 0; // this is the size of that buffer;
hrRet = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
bitmap.Save(pStream, &clsid, &encoderParameters);
hrRet = pStream->Seek(liZero, STREAM_SEEK_SET, &pos);
hrRet = pStream->Stat(&stg, STATFLAG_NONAME);
dwBufferSize = stg.cbSize.LowPart;
// copy the stream into memory
hrRet = pStream->Read(buffer, stg.cbSize.LowPart, &bytesRead);

}
}

sendto() 函数是:

 sendto(s,buffer,sizeof(buffer) , 0 , (struct sockaddr *) &si_other, slen)

最佳答案

终于解决了...就像保存文件一样简单

std::ofstream("D:\\abc.jpg", std::ios::binary).write(buf,recv_len); 

这里第一个参数是保存文件的位置,第二个是操作模式,因为我们在这里处理图像数据,选择二进制。写入方法不言自明

关于c++ - 从字符数组中检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21656878/

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