1

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)
Community
  • 1
  • 1
Ganesh
  • 251
  • 2
  • 13
  • Have you tried memcpy()? – Novin Shahroudi Feb 09 '14 at 08:40
  • What is the code that attempts to convert it back again? – Mats Petersson Feb 09 '14 at 08:42
  • First af all try to detect what part of your code has a problem: screenshot extracting or data transfer via sendto/recv. Compare binary data before sending and just after receiving. – KonstantinL Feb 09 '14 at 08:45
  • i have not been able to write code - the socket function which receives above sent character data is recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)).Here buf is also character array – Ganesh Feb 09 '14 at 08:45
  • @KonstantinL the problem does not lies in code provided by me.I just want to convert the character array received back into image data. – Ganesh Feb 09 '14 at 08:48
  • Have you tried reinterpret_cast, not the best way but might help? – macroland Feb 09 '14 at 08:52
  • When you use sendto, is buffer still an array or a pointer. If it is a pointer, then sizeof will only give you the size of the pointer rather than the size of the array. Also, shouldn't the size be bytesRead instead of sizeof(buffer)? – cup Feb 09 '14 at 09:12
  • @cup buffer is array in sendto() and i have debugged sizeof() function gives correct size.And yes bytesRead is more appropriate to use.Still that is not related to my problem.I want how to go ahead with this array conversion into image file on receiver side – Ganesh Feb 09 '14 at 09:19

1 Answers1

1

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

Ganesh
  • 251
  • 2
  • 13