Gdiplus can save to file, or save to memory using IStream. See Gdiplus::Image::Save method//get gdi+ bitmapGdiplus::Bitmap bitmap(hbitmap, nullptr);//write to IStreamIStream* istream = nullptr;HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &istream);CLSID clsid_png;CLSIDFromString(L"{557cf406-1a04-11d3-9a73-0000f81ef32e}", &clsid_png);bitmap.Save(istream, &clsid_png);内存大小足够小,您可以将其从IStream复制到单个缓冲区:The memory size is small enough that you can copy from IStream to a single buffer://copy IStream to bufferint bufsize = GlobalSize(hg);char *buffer = new char[bufsize];//lock & unlock memoryLPVOID ptr = GlobalLock(hg);memcpy(buffer, ptr, bufsize);GlobalUnlock(hg);//release will automatically free the memory allocated in CreateStreamOnHGlobalistream->Release(); PNG现在在buffer中可用,其大小为bufsize.您可以直接使用二进制数据,也可以转换为Base64以通过网络发送PNG is now available in buffer, its size is bufsize. You can work directly with the binary data, or convert to Base64 to send over the network...delete[]buffer; MCVE:#include <iostream>#include <fstream>#include <vector>#include <Windows.h>#include <gdiplus.h>bool save_png_memory(HBITMAP hbitmap, std::vector<BYTE> &data){ Gdiplus::Bitmap bmp(hbitmap, nullptr); //write to IStream IStream* istream = nullptr; CreateStreamOnHGlobal(NULL, TRUE, &istream); CLSID clsid_png; CLSIDFromString(L"{557cf406-1a04-11d3-9a73-0000f81ef32e}", &clsid_png); Gdiplus::Status status = bmp.Save(istream, &clsid_png); if(status != Gdiplus::Status::Ok) return false; //get memory handle associated with istream HGLOBAL hg = NULL; GetHGlobalFromStream(istream, &hg); //copy IStream to buffer int bufsize = GlobalSize(hg); data.resize(bufsize); //lock & unlock memory LPVOID pimage = GlobalLock(hg); memcpy(&data[0], pimage, bufsize); GlobalUnlock(hg); istream->Release(); return true;}int main(){ CoInitialize(NULL); ULONG_PTR token; Gdiplus::GdiplusStartupInput tmp; Gdiplus::GdiplusStartup(&token, &tmp, NULL); //take screenshot RECT rc; GetClientRect(GetDesktopWindow(), &rc); auto hdc = GetDC(0); auto memdc = CreateCompatibleDC(hdc); auto hbitmap = CreateCompatibleBitmap(hdc, rc.right, rc.bottom); auto oldbmp = SelectObject(memdc, hbitmap); BitBlt(memdc, 0, 0, rc.right, rc.bottom, hdc, 0, 0, SRCCOPY); SelectObject(memdc, oldbmp); DeleteDC(memdc); ReleaseDC(0, hdc); //save as png std::vector<BYTE> data; if(save_png_memory(hbitmap, data)) { //write from memory to file for testing: std::ofstream fout("test.png", std::ios::binary); fout.write((char*)data.data(), data.size()); } DeleteObject(hbitmap); Gdiplus::GdiplusShutdown(token); CoUninitialize(); return 0;} 这篇关于C ++ gdi :: Bitmap到内存中的PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!