HBITMAP转成CBitmap:
CBitmap* pcBmp = CBitmap::FromHandle(hBmp);
CBitmap bmp;
bmp.Attach(hbmp);
CBitmap转成HBITMAP:
HBITMAP hBmp = (HBITMAP)cBmp;
CBitmap* pBmp;
HBITMAP hBmp = (HBITMAP)(*pBmp);//(HBITMAP)是CBitmap类重载了的操作符.
(1)
//FileName:路径名称
HBITMAP General::LoadALLPIC(char * FileName)
{
IPicture* p=NULL;
IStream* s=NULL;
HGLOBAL hG;
void* pp;
FILE* fp;
// Read file in memory
fp = fopen(FileName,"rb");//打开文件
if (!fp)
{
return NULL;
}
fseek(fp,0,SEEK_END);
int fs = ftell(fp);
fseek(fp,0,SEEK_SET);
hG = GlobalAlloc(GPTR,fs);
if (!hG)
{
fclose(fp);
return NULL;
}
pp = (void*)hG;
fread(pp,1,fs,fp);
fclose(fp);
CreateStreamOnHGlobal(hG,false,&s);
if (!s)
{
GlobalFree(hG);
return NULL;
}
OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);
if (!p)
{
s->Release();
GlobalFree(hG);
return NULL;
}
s->Release();
GlobalFree(hG);
HBITMAP hB = 0;
p->get_Handle((unsigned int *)&hB);
// Copy the image.Necessary, because upon p's release,
// the handle is destroyed.
HBITMAP hBB = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG );
p->Release();
return hBB;
}
(2)
// FileName: 待载入的图片的完整路径
//成功返回bitmap句柄,否则返回NULL
//出ico格式,其他格式图片均可
HBITMAP LoadALLPIC(char * FileName)
{
CImage img;
CString strPath;
strPath = FileName;
HRESULT ret = img.Load(strPath); // filename 是要加载的文件名(包含路径)
if( !img.IsNull())
{
HBITMAP hBitmap = img.Detach();
return hBitmap;
}
else
{
return NULL;
}
}
(3)
//装载图片(非png格式)
//由导入的图片资源ID装载
//hBitmap = LoadPic(MAKEINTRESOURCE(IDR_GIF1), _T("GIF"));//调用
HBITMAP CProtectScreenDlg::LoadPic(LPCTSTR szResourceName, LPCTSTR szResourceType)
{
ASSERT(szResourceName);
ASSERT(szResourceType);
HRSRC hPicture = FindResource(AfxGetResourceHandle(),szResourceName,szResourceType);
HGLOBAL hResData;
if (!hPicture || !(hResData = LoadResource(AfxGetResourceHandle(),hPicture)))
{
TRACE(_T("Load (resource): Error loading resource %s/n"),szResourceName);
return FALSE;
};
DWORD dwSize = SizeofResource(AfxGetResourceHandle(),hPicture);
// hResData is not the real HGLOBAL (we can't lock it)
// let's make it real
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize);
if (!hGlobal)
{
TRACE(_T("Load (resource): Error allocating memory/n"));
FreeResource(hResData);
return FALSE;
};
char *pDest = reinterpret_cast<char *> (GlobalLock(hGlobal));
char *pSrc = reinterpret_cast<char *> (LockResource(hResData));
if (!pSrc || !pDest)
{
TRACE(_T("Load (resource): Error locking memory/n"));
GlobalFree(hGlobal);
FreeResource(hResData);
return FALSE;
};
CopyMemory(pDest,pSrc,dwSize);
FreeResource(hResData);
GlobalUnlock(hGlobal);
// BOOL bRetValue = Load(hGlobal,dwSize);
// GlobalFree(hGlobal);
// return bRetValue;
IPicture* p=NULL;
IStream* s=NULL;
CreateStreamOnHGlobal(hGlobal,false,&s);
if (!s)
{
GlobalFree(hResData);
return NULL;
}
OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);
if (!p)
{
s->Release();
GlobalFree(hGlobal);
return NULL;
}
s->Release();
GlobalFree(hGlobal);
HBITMAP hB = 0;
p->get_Handle((unsigned int *)&hB);
// Copy the image.Necessary, because upon p's release,
// the handle is destroyed.
HBITMAP hBB = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG );
p->Release();
return hBB;
}
(4)除ico外的图片格式
HBITMAP LoadALLPIC(CString strFileName)
{
CImage img;
HRESULT ret = img.Load(strFileName); // filename 是要加载的文件名(包含路径)
if( !img.IsNull())
{
HBITMAP hBitmap = img.Detach();
return hBitmap;
}
else
{
return NULL;
}
}