显示位图文件有n中方法:
1 利用双缓冲技术;
源码:
void showBMP()
{
CString str;
CString filter="Bitmap Files(*.bmp)|*.bmp|Wmf Files(*.wmf)|*.wmf||";
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,(LPCTSTR)filter,NULL);
if (dlg.DoModal() == IDOK)
{
str=dlg.GetPathName();
HBITMAP hBitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),str,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bitmap;
bitmap.Attach(hBitmap); //关联位图对象
CDC* pDC=GetDC();
CDC dcimage;
dcimage.CreateCompatibleDC(pDC); //内存DC
BITMAP bmp;
bitmap.GetBitmap(&bmp); //获取位图信息
dcimage.SelectObject(&bitmap); //选取位图对象
pDC->BitBlt(0,0,bmp.bmWidth,bmp.bmHeight,&dcimage,0,0,SRCCOPY); //显示
}
};
2 使用 Cximage .draw()
源码:
void CMap2Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
CxImage image;
// CImage Image;
CString fileName;
CString filter="JPEG Files(*.jpg)|*.jpg|Bitmap Files(*.bmp)|*.bmp|Wmf Files(*.wmf)|*.wmf||";
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,(LPCTSTR)filter,NULL);
if (dlg.DoModal() == IDOK)
{
fileName=dlg.GetFileName();
CString fileExt; //图片的扩展名
int len = fileName.GetLength();
for(int i=len-1; i>=0; i--) //得到图片的扩展名
{
if(fileName[ i ] == '.')
{
fileExt=fileName.Mid(i+1);
break;
}
}
fileExt.MakeLower(); //将扩展名转为小写
if(fileExt != _T(""))
{
//创建CxImage对象,其中静态方法CxImage::GetTypeIdFromName用来根据扩展名获得图片格式的ID代表
CxImage image(fileName,CxImage::GetTypeIdFromName(fileExt));/** / CXIMAGE_FORMAT_JPG);*/
if(image.IsValid())
{
HDC hdc=(HDC)GetForegroundWindow();
CDC * pDc=GetDC();
CRect rect;
GetWindowRect(&rect);
CPaintDC dc(this);
image.Draw(dc.GetSafeHdc(), rect,0, 0);
image.Destroy();
}
}
}
}