//从资源中加载图片
Image* GDIGetImageFromResource(char * RES_ID)
{
//LPCTSTR pResourceName = MAKEINTRESOURCE(nResourceID);
//HMODULE hInstance = ahInstance;
HRSRC hResource = FindResource(HInstance,RES_ID,RT_RCDATA);
if(!hResource)
{
ShowMessage("未找到资源");
return NULL;
}
//取得资源大小
DWORD dwResourceSize = SizeofResource(HInstance, hResource);
if(!dwResourceSize)
{
return NULL;
}
const void* pResourceData = LockResource(LoadResource(HInstance, hResource));
if(!pResourceData)
{
return NULL;
}
HGLOBAL hResourceBuffer = GlobalAlloc(GMEM_MOVEABLE, dwResourceSize);
if(!hResourceBuffer)
{
GlobalFree(hResourceBuffer);
return NULL;
}
void* pResourceBuffer = GlobalLock(hResourceBuffer);
if(!pResourceBuffer)
{
GlobalUnlock(hResourceBuffer);
GlobalFree(hResourceBuffer);
return NULL;
}
//把资源拷贝到分配的内存中
CopyMemory(pResourceBuffer, pResourceData, dwResourceSize);
IStream* pIStream = NULL;
if(CreateStreamOnHGlobal(hResourceBuffer, FALSE, &pIStream)==S_OK)
{
Image *pImage = Gdiplus::Image::FromStream(pIStream);
pIStream->Release();
GlobalUnlock(hResourceBuffer);
GlobalFree(hResourceBuffer);
return pImage;
}
return NULL;
}
//从资源中显示图片
//资源类型:Icon,Cursor,Bitmap,String,Accelerators,Menu,Dialog,RCData,Version info
void GdiDrawImageFromRes(TRect &r,char * RES_ID)
{
Image *img = GDIGetImageFromResource(RES_ID);
if(img)
{
if(myGP)
{
myGP->DrawImage(img,(int)r.left,(int)r.top,(int)r.right,(int)r.bottom);
delete img;
}
}
}
资源文件.RC内容如下:
/*********************************************
File: GDIRES.RC
Generated by Resource Builder (3.0.2.22).
*********************************************/
/*
OutputExt=res
*/
#define IDS_STRING0 1
STRINGTABLE
MOVEABLE PURE LOADONCALL DISCARDABLE
LANGUAGE LANG_NEUTRAL, 0
{
IDS_STRING0, "GMIS网络消息"
}
RCDATA_0 RCDATA
MOVEABLE PURE LOADONCALL DISCARDABLE
LANGUAGE LANG_NEUTRAL, 0
{
'89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 '
'00 00 00 10 00 00 00 10 08 06 00 00 00 1F F3 FF '
'61 00 00 00 04 73 42 49 54 08 08 08 08 7C 08 64 '
'88 00 00 00 09 70 48 59 73 00 00 00 27 00 00 00 '
'27 01 2A 09 91 4F 00 00 00 16 74 45 58 74 43 72 '
'65 61 74 69 6F 6E 20 54 69 6D 65 00 30 38 2F 30 '
'36 2F 31 35 61 28 33 15 00 00 00 1C 74 45 58 74 '
'53 6F 66 74 77 61 72 65 00 41 64 6F 62 65 20 46 '
'69 72 65 77 6F 72 6B 73 20 43 53 36 E8 BC B2 8C '
'00 00 02 0E 49 44 41 54 38 8D 9D D0 CF 4B 14 71 '
GDI+从资源中的加载PNG图片
最新推荐文章于 2018-07-04 10:26:00 发布