MFC完成图片查看器的功能 |
您所在的位置:网站首页 › 图片时间查看器 › MFC完成图片查看器的功能 |
MFC完成图片查看器的功能
预期目标:
1.打开同类型的位图,文件按顺序排列时可顺序打开下一张图像; 2.用鼠标滚轮完成图像的放大及缩小,且以鼠标所指的像素点为固定像素点; 3.显示图像的绝对像素坐标和相对像素坐标,并且显示图像的放大比例。 需要注意的点:1.在放大的时候,会遇到图像上、下、左、右侧超出显示控件的情况,因此在放大的时候,需要判断图像边界位置。 2.在放大及缩小的时候要保证图像长宽同比变换,即保证图像不失真。 3.在找出图像绝对像素坐标和相对像素坐标时,要注意比例转换。 4.开始楼主敲得代码,在缩放图像时,总是出现闪屏的现象,后来才发现原因,不能直接在控件里放置图像,需要在缓存一 次,就解决闪屏现象。 效果图如下:打开文件。 void CImageViewDlg::OnBnClickedOpenFile() { CString filter = L"所有文件(*.bmp,*.jpg,*.tiff)|*.bmp;*.jpg;*.gif;*.tiff| BMP(*.bmp)|*.bmp|JPG(*.jpg)|*.jpg|GIF(*.gif)|TIFF(*.tiff)|*.tiff"; CFileDialog Ofile(TRUE, NULL, NULL, OFN_HIDEREADONLY, filter, NULL); g_iZoom = 1; g_iRadio = 0.5; if (IDOK == Ofile.DoModal()) { FilePath = Ofile.GetPathName(); CString fEnt = Ofile.GetFileExt(); End_name = fEnt.MakeLower(); File_Folder = FilePath.Left(FilePath.GetLength() - End_name.GetLength() - 2); CString tmp_count = FilePath.Left(FilePath.GetLength() - End_name.GetLength() - 1); tmp_count = tmp_count.Right(1); count = _ttoi(tmp_count); CWnd* pWnd = GetDlgItem(IDC_Picture); CDC* pDC = pWnd->GetDC(); HDC hDC = pDC->m_hDC; pWnd->GetClientRect(&picRect); Image.Destroy(); Image.Load(FilePath); Image_Rat = (float)Image.GetWidth() / (float)Image.GetHeight(); RectSize(Image, picRect, curRect, Image_scale); ::SetStretchBltMode(hDC, HALFTONE); ::SetBrushOrgEx(hDC, 0, 0, NULL); ZoomImage(Image, picRect, curRect, g_iZoom, Image_scale); SetDlgItemText(IDC_Text1, FilePath); ReleaseDC(pDC); } // TODO: 在此添加控件通知处理程序代码 } void CImageViewDlg::OnBnClickedNextFile() { CString filter = L"所有文件(*.bmp,*.jpg,*.tiff)|*.bmp;*.jpg;*.gif;*.tiff| BMP(*.bmp)|*.bmp|JPG(*.jpg)|*.jpg|GIF(*.gif)|TIFF(*.tiff)|*.tiff"; CFileDialog Ofile(TRUE, NULL, NULL, OFN_HIDEREADONLY, filter, NULL); CString tmp_Path, tmp_count; g_iZoom = 1; g_iRadio = 0.5; count++; tmp_count.Format(_T("%01d."), count); FilePath = File_Folder + tmp_count + End_name; CWnd* pWnd; pWnd = GetDlgItem(IDC_Picture); CDC* pDC = pWnd->GetDC(); HDC hDC = pDC->m_hDC; pWnd->GetClientRect(&picRect); Image.Destroy(); File_Open = Image.Load(FilePath); if (File_Open != 0) { count--; tmp_count.Format(_T("%01d."), count); FilePath = File_Folder + tmp_count + End_name; File_Open = Image.Load(FilePath); File_Open = 2; return; } Image_Rat = (float)Image.GetWidth() / (float)Image.GetHeight(); RectSize(Image, picRect, curRect, Image_scale); ::SetStretchBltMode(hDC, HALFTONE); ::SetBrushOrgEx(hDC, 0, 0, NULL); ZoomImage(Image, picRect, curRect, g_iZoom, Image_scale); SetDlgItemText(IDC_Text1, FilePath); ReleaseDC(pDC); // TODO: 在此添加控件通知处理程序代码 }缩放到边缘时,避免图像移出显示窗口。 void CImageViewDlg::RectSize(CImage &inputImage, CRect &inputRect, CRect &outputRect, float &imput_scale) { float in_x, in_y; in_x = inputRect.Width(); in_y = inputRect.Height(); float rows, cols; rows = inputImage.GetHeight(); cols = inputImage.GetWidth(); float rat = cols / rows; if (rows > in_y && cols in_y && cols > in_x) { if ((rows / in_y) >= (cols / in_x)) { imput_scale = rows / in_y; outputRect.left = (in_x - (in_y * rat)) / 2; outputRect.right = outputRect.left + (in_y * rat); outputRect.top = 0; outputRect.bottom = in_y; } else if ((rows / in_y) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |