从字符数组中检索图像

时间:2014-02-09 08:21:28

标签: c++ image visual-c++ winsock

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

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)

1 个答案:

答案 0 :(得分:1)

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

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

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