简体   繁体   中英

Converting a screenshot bitmap to jpeg in memory

I am trying to take a screenshot of the whole screen and would like to send it over the network to a server. (the server will be the one to save it)

I am currently using this code

HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h);

CImage image;
image.Attach(hBitmap);
image.Save(L"screen.jpg");

It does save the file, but that's not what I want. I'm also not sure how to get the bytes, I was thinking of GetBits() but the documentation says it is a pointer to the data of the underlying Bitmap.

What I want to get is the JPG data which I can send through the network and save the image using the server.

So, why am I not just using bitmap and sending it directly? because it is a bit complicated and the code was quite long, you'll have to get the header and stuff. I could be wrong though.

My question:
- How do I get the bytes from the image so I can send it to the server? (like a BLOB to save to the database)
- Any library that can help me convert and get bytes all in memory?

references:
https://msdn.microsoft.com/en-us/library/ms535406(v=VS.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms535406(v=vs.85).aspx
http://www.codeproject.com/Tips/738533/save-load-image-between-buffer

So Dan pointed out that there is actually a method Save that can use a Stream. I've decided to use this approach, which I got from one of the references listed in my question

vector<BYTE> buf;
IStream *stream = NULL;
HRESULT hr = CreateStreamOnHGlobal(0, TRUE, &stream);
CImage image;
ULARGE_INTEGER liSize;

// screenshot to jpg and save to stream
image.Attach(hBitmap);
image.Save(stream, Gdiplus::ImageFormatJPEG);
IStream_Size(stream, &liSize);
DWORD len = liSize.LowPart;
IStream_Reset(stream);
buf.resize(len);
IStream_Read(stream, &buf[0], len);
stream->Release();

// just testing if the buf contains the correct data
fstream fi;
fi.open("aaashot.jpg", fstream::binary | fstream::out);
fi.write(reinterpret_cast<const char*>(&buf[0]), buf.size()*sizeof(BYTE));
fi.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM