C++博客

CYourClass::~CYourClass()
{
for(IMG_VECTOR::iterator it = m_arImage.begin(); it != m_arImage.end(); it++)
delete *it;
for(HGLB_VECTOR::iterator it = m_arGlobal.begin(); it != m_arGlobal.end(); it++)
{
::GlobalUnlock(*it);
::GlobalFree(*it);
}
}
void CYourClass::AddImage(HMODULE hInst, UINT nResourceID, LPCTSTR lpType)
{
if(lpType == RT_BITMAP)
{
//GDI+ can not load RT_BITMAP resouce,
//because they are predefined resource,
//they don't contains the image file header.
assert(FALSE);
return;
}
HRSRC hResource = ::FindResource(hInst, MAKEINTRESOURCE(nResourceID), lpType);
if (!hResource)
return;
DWORD imageSize = ::SizeofResource(hInst, hResource);
if (!imageSize)
return;
const void* pResourceData = ::LockResource(::LoadResource(hInst, hResource));
if (!pResourceData)
return;
HGLOBAL hBuffer = ::GlobalAlloc(GMEM_FIXED, imageSize);
if (NULL == hBuffer)
return;
void* pBuffer = ::GlobalLock(hBuffer);
if (pBuffer)
{
CopyMemory(pBuffer, pResourceData, imageSize);
IStream* pStream = NULL;
if (::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream) == S_OK)
{
Gdiplus::Image * pImage = Gdiplus::Image::FromStream(pStream);
pStream->Release();
if (pImage)
{
if (pImage->GetLastStatus() == Gdiplus::Ok &&
pImage->GetWidth() > 0)
{
m_arImage.push_back(pImage);
//it seems the image will take usage of the global memory.
//so the global memory should be kept until the image destroy.
m_arGlobal.push_back(hBuffer);
return;
}
delete pImage;
}
}
::GlobalUnlock(hBuffer);
}
::GlobalFree(hBuffer);
本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕,E-mail:xinmeigg88@163.com
本文链接:http://www.ksxb.net/tnews/4449.html