Bitmap::FromFile方法在x86上返回NULL



我有一段在Windows 7 64位上工作的代码:它允许我将包含在std::stringBase64EncodedImage)中的Image的表示转换为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;    
}

但它在Windows 7 32位上失败了,特别是在以下行:

bitmap = Bitmap::FromStream(pStream);

它总是返回NULL,但我不知道这是如何在x64上工作的,而不是在x86中。如果有人能启发我,我将不胜感激。

谢谢!

您提供的代码对我来说效果很好。

但是当我对GDI+初始化进行评论时,Bitmap::FromStream(pStream)方法总是返回NULL指针。

你有GDI+初始化吗?

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

顺便说一下,GDI+取消初始化:

GdiplusShutdown(gdiplusToken);

最新更新