Gdiplus::Bitmap从内存加载/保存到内存

Gdiplus::Bitmap从内存加载

std::string img_data;
IStream* img_stream = ::SHCreateMemStream((const BYTE*)img_data.c_str(), img_data.size()));
Gdiplus::Bitmap* bitmap = Gdiplus::Bitmap::FromStream(img_stream);

Gdiplus::Bitmap保存到内存

Gdiplus::Bitmap bitmap;
IStream* img_stream = ::SHCreateMemStream(nullptr, 0);
Gdiplus::Status status = bitmap.save(img_stream, Gdiplus::ImageFormatJPEG);
if (status == Gdiplus::Ok) {
  ULARGE_INTEGER uli;
  HRESULT ret = ::IStream_Size(img_stream, &uli);
  if (ret == S_OK) {
    LARGE_INTEGER liMove;
    ZeroMemory(&liMove, sizeof(LARGE_INTEGER));
    img_stream->Seek(liMove, STREAM_SEEK_SET, NULL);

    std::string img_data;
    img_data.resize(uli.LowPart);
    ret = ::IStream_Read(img_stream, &img_data[0], uli.LowPart);
  }
}

以上是demo代码,使用时需要注意返回值处理和对象释放问题