Bitmap :: FromFile方法在x86上返回NULL [英] Bitmap::FromFile method returns NULL on x86
问题描述
我的这段代码在Windows 7 64位上运行:它允许我将 Image
的表示形式转换为 std :: string
( Base64EncodedImage
)到 GdiPlus :: Bitmap
:
HRESULT hr;
使用命名空间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); //在WIN32 $ FAILS
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位上失败,特别是在这行: / p>
bitmap = Bitmap :: FromStream(pStream);
它总是返回 NULL
得到这是如何工作在x64,但不是在x86。如果有人可以启发我,我会很感激。
谢谢!
但是当我评论了GDI +初始化时, Bitmap :: FromStream(pStream)
方法总是返回 NULL
指针。
初始化?
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
//初始化GDI +。
GdiplusStartup(& gdiplusToken,& gdiplusStartupInput,NULL);
顺便说一下,GDI +未初始化:
GdiplusShutdown(gdiplusToken);
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);
这篇关于Bitmap :: FromFile方法在x86上返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!