C++从资源中加载jpg,png文件

void  ImageFromIDResource(CImage& image, UINT nID, LPCTSTR lpType)  
{  
    HINSTANCE hInst = g_hInst;  
    HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),lpType);   
    if(hRsrc == NULL)  
        return;  
    DWORD dwLen = SizeofResource(hInst, hRsrc);  
    BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);  
    if (!lpRsrc)  
        return;;  
    HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, dwLen);  
    BYTE* pmem = (BYTE*)GlobalLock(m_hMem);  
    memcpy(pmem,lpRsrc,dwLen);  
    IStream* pstm;  
    CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);  
    image.Load(pstm);  
    GlobalUnlock(m_hMem);  
    pstm->Release();  
    FreeResource(lpRsrc);  
}
posted @ 2012-04-27 12:35  likebeta  阅读(2675)  评论(0编辑  收藏  举报