I've this piece of code working on Windows 7 64-bit: it allows me to transform a representation of an Image
contained into a std::string
(Base64EncodedImage
) to a GdiPlus::Bitmap
:
HRESULT hr;
using namespace Gdiplus;
std::string decodedImage = Base64EncodedImage;
DWORD imageSize = decodedImage.length();
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
if (!hMem)
ErrorExit(TEXT("GlobalAlloc")); //http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
LPVOID pImage = ::GlobalLock(hMem);
if (!pImage)
ErrorExit(TEXT("GlobalLock"));
CopyMemory(pImage, decodedImage.c_str(), imageSize);
IStream* pStream = NULL;
BitmapData* bitmapData = new BitmapData;
if (::CreateStreamOnHGlobal(hMem, FALSE, &pStream) != S_OK)
ErrorExit(TEXT("CreateStreamOnHGlobal"));
else
{
bitmap = Bitmap::FromStream(pStream); //FAILS on WIN32
if (!bitmap)
ErrorExit(TEXT("FromStream"));
RECT clientRect;
GetClientRect(hwnd, &clientRect);
bitmapClone = bitmap->Clone(0, 0, clientRect.right, clientRect.bottom, PixelFormatDontCare);
delete bitmap;
bitmap = NULL;
}
But it fails on Windows 7 32-bit, specifically on this line:
bitmap = Bitmap::FromStream(pStream);
It always returns NULL
, but I can't get how is this working on x64 but not in x86. If someone can enlighten me, I'll be grateful.
Thanks!
The code you've provided works well for me.
But when I've commented the GDI+ initialization, the Bitmap::FromStream(pStream)
method always returns NULL
pointer.
Do you have the GDI+ initialization?
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
By the way, the GDI+ uninitialization:
GdiplusShutdown(gdiplusToken);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With