I am using following gdiscreen() function which takes 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() of socket is:

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

It is by far easier to work with Bitmaps. I'm not used to GDI+ however, does your socket send all the bytes?

If you use Bitmaps, then the following can work for you:

HBITMAP PixelsToHBitmap(void* Pixels, int Width, int Height, unsigned short BitsPerPixel)
{
    void* Memory = nullptr;
    HBITMAP ImageHandle = nullptr;
    BITMAPINFO Info = {{sizeof(BITMAPINFOHEADER), Width, -Height, 1, BitsPerPixel, BI_RGB, 0, 0, 0, 0, 0}};
    ImageHandle = CreateDIBSection(nullptr, &Info, DIB_RGB_COLORS, &Memory, nullptr, 0);
    memcpy(Memory, Pixels, Width * Height * 4);
    return ImageHandle;  //DO NOT FORGET TO DeleteObject later!
}

If you still wish to use JPEG, I'm sure it won't be too hard to look up how to get an hBitmap from Pixels for it..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.