注意:picture control控件,需要先更改其ID再使用。
CImage myImage; CFileDialog fileDlg(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,_T("Picture Files (*.bmp *.jpg)|*bmp;;*jpg||"),AfxGetMainWnd()); CString pathName; if(fileDlg.DoModal () == IDOK) { POSITION mPos = fileDlg.GetStartPosition(); if(mPos!=NULL) { pathName = (LPCTSTR)fileDlg.GetPathName(); if(!myImage.IsNull())//判断图象是否为空,如果不为空则先释放掉 myImage.Destroy(); myImage.Load(pathName); } } CRect rect; CWnd *pWnd = GetDlgItem(IDC_MY_PIC2); CDC *pDC = pWnd->GetDC(); //第1个控件 pWnd->GetClientRect(&rect); //取得客户区尺寸 pDC->SetStretchBltMode(STRETCH_HALFTONE); //保持图片不失真 myImage.Draw( pDC->m_hDC,rect); //已控件尺寸大小来绘图 ReleaseDC( pDC ); myImage.Destroy();