本人写的,可以打开BMP,JPEG,GIF类型
BOOL ShowImageFromFile(CDC *pDC,UINT nLeft,UINT nTop,UINT nWidth,UINT nHeight,const CString &strFileName)
{
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
if(
file.Open(strFileName,CFile::modeRead)&&
file.GetStatus(strFileName,fstatus)&&
((cb = (LONG)fstatus.m_size) != -1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE,cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal))!= NULL)
{
file.Read(pvData,cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
IPicture *pPic;
if(SUCCEEDED(OleLoadPicture(pStm,(LONG)fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
double fX,fY;
fX =(double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
fY =(double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
if(FAILED(pPic->Render(pDC->m_hDC,nLeft,nTop,nWidth,nHeight,
0,hmHeight,hmWidth,-hmHeight,NULL)))
AfxMessageBox("读入图像数据失败!");
pPic->Release();
}
else
{
AfxMessageBox("加载图像失败!");
return FALSE;
}
}
}
else
{
AfxMessageBox("打开图像文件失败!");
return FALSE;
}
}
return TRUE;
}