此页面上的内容需要较新版本的 Adobe Flash Player。

获取 Adobe Flash Player

您现在的位置: 智可网 - 程序开发 - 微软开发专区 - Visual C - 正文
利用VC实现JPG图片加载
教程录入:admin    责任编辑:admin 作者:佚名 文章来源:互联网
 在OnDraw()函数中添加如下的代码,并在工程目录下放入test.jpg的图片。

IPicture *pPic;

IStream *pStm;

CFileStatus fstatus;

CFile file;

LONG cb;

if (file.Open("test.jpg",CFile::modeRead)&&file.GetStatus("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;

CRect rect;

GetClIEntRect(rect);

fX=rect.Width();

fY=rect.Height();

//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,0,0,(DWORD)fX,(DWord)fY,0,hmHeight,hmWidth,-hmHeight,NULL))) AfxMessageBox("Failed To Render The picture!");

pPic->Release();

}

else

AfxMessageBox("Error Loading Picture From Stream!");

}

}

}

else

AfxMessageBox("Can't Open Image File!");
分享
打赏我
打开支付宝"扫一扫" 打开微信"扫一扫"
客户端
"扫一扫"下载智可网App
意见反馈
利用VC实现JPG图片加载
作者:佚名 来源:互联网
 在OnDraw()函数中添加如下的代码,并在工程目录下放入test.jpg的图片。

IPicture *pPic;

IStream *pStm;

CFileStatus fstatus;

CFile file;

LONG cb;

if (file.Open("test.jpg",CFile::modeRead)&&file.GetStatus("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;

CRect rect;

GetClIEntRect(rect);

fX=rect.Width();

fY=rect.Height();

//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,0,0,(DWORD)fX,(DWord)fY,0,hmHeight,hmWidth,-hmHeight,NULL))) AfxMessageBox("Failed To Render The picture!");

pPic->Release();

}

else

AfxMessageBox("Error Loading Picture From Stream!");

}

}

}

else

AfxMessageBox("Can't Open Image File!");