在VC中显示JPEG和GIF图像
【打印文章】
我看了MSDN也以为只对BMP有用,结果是对JPG GIF都可以。
向不轻信MS的JDK致敬!
//admire JDK
以下是实验代码,大家可以在95下试试,我在98+IE5上成功。
是一个CView的例子,别忘了AfxOleInit
void CPICView::OnDraw(CDC* pDC)
{
CPICDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
IPicture *pPic;
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
if (file.Open("c:/test.jpg",CFile::modeRead)&&file.GetStatus("c:/test.jpg",
fstatus)&&
((cb = fstatus.m_size) != -1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal)) != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
if(SUCCEEDED(OleLoadPicture(pStm,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->Ge
tDeviceCaps(HORZSIZE)*100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->G
etDeviceCaps(VERTSIZE)*100.0);
if(FAILED(pPic->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-h
mHeight,NULL)))
AfxMessageBox("Failed To Render The picture!");
pPic->Release();
}
else
AfxMessageBox("Error Loading Picture From Stream!");
}
}
}
else
AfxMessageBox("Can't Open Image File!");
}
向不轻信MS的JDK致敬!
//admire JDK
以下是实验代码,大家可以在95下试试,我在98+IE5上成功。
是一个CView的例子,别忘了AfxOleInit
void CPICView::OnDraw(CDC* pDC)
{
CPICDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
IPicture *pPic;
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
if (file.Open("c:/test.jpg",CFile::modeRead)&&file.GetStatus("c:/test.jpg",
fstatus)&&
((cb = fstatus.m_size) != -1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal)) != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
if(SUCCEEDED(OleLoadPicture(pStm,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->Ge
tDeviceCaps(HORZSIZE)*100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->G
etDeviceCaps(VERTSIZE)*100.0);
if(FAILED(pPic->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-h
mHeight,NULL)))
AfxMessageBox("Failed To Render The picture!");
pPic->Release();
}
else
AfxMessageBox("Error Loading Picture From Stream!");
}
}
}
else
AfxMessageBox("Can't Open Image File!");
}
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】