سؤال

I have adapted part of this block of code from answer post herewhich save screenshot jpg image into buffer.I am sending this buffered image using sento() over UDP.I am unable to convert this char array data sent back into image .I appreciate any help you can provide

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() function is:

 sendto(s,buffer,sizeof(buffer) , 0 , (struct sockaddr *) &si_other, slen)
هل كانت مفيدة؟

المحلول

Finally solved...it as simple as saving file

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

here first param is location for saving file,second is mode of operation as here we are dealing with image data binary is chosen.write method is self explanatory

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top