Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap::FromFile method returns NULL on x86

Tags:

c++

gdi+

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!

like image 489
Maximiliano Santa Cruz Avatar asked Feb 06 '12 16:02

Maximiliano Santa Cruz


1 Answers

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);
like image 165
Sergey Vyacheslavovich Brunov Avatar answered Sep 30 '22 07:09

Sergey Vyacheslavovich Brunov