显示JPG格式图片的c++程序,运行后,图片没有显示在窗口里,而是出错显示在整个windows窗口,为什么?
显示JPG格式图片的c++程序,运行后,图片没有显示在窗口里,而是出错显示在整个windows窗口,为什么?
请高人帮忙指点下,哪出错了,谢谢!
程序代码如下:
void CMainFrame::OnFileOpen()
{
// TODO: Add your command handler code here
//打开图片路径,并选择
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Jpeg Files (*.jpg;*.jpeg)|*.jpg; *.jpeg|", NULL);
if (dlg.DoModal() == IDOK)
{
m_strSourceFile = dlg.GetPathName();
if (file.Open(m_strSourceFile,CFile::modeRead)&&file.GetStatus(m_strSourceFile,fstatus)&& ((cb = fstatus.m_size) != -1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
pvData = GlobalLock(hGlobal);
if (pvData != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
}
}
}
else
{
return;
}
//显示图片
IPicture *pPic;
CoInitialize(NULL);
CDC* pDC = m_pic1.GetDC(); //获得显示控件的DC
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;
//get image height and width
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);
//get image position
m_pic1.GetWindowRect(&rect1);
//use render function display image
if(FAILED(pPic->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
{
pPic->Release();
return;
}
pPic->Release();
}
else
{
return;
}
CoUninitialize();
return;
}
}
[解决办法]
显示 部分怎么不写在view的OnDraw 里呢?
[解决办法]
这个一般是坐标的问题, 你先检查一下你获取到的窗口区域是不是你要的
[解决办法]