您的位置:首页 > 其它

简单的MFC在Picture控件上放大缩小图像,显示时:缩小功能正常,放大功能有问题

2016-11-30 14:50 411 查看
//载入原始图
Mat srcImage = imread("D:\\1.bmp",1);  //工程目录下应该有一张名为1.jpg的素材图
Mat tmpImage,dstImage1,dstImage2;//临时变量和目标图的定义
tmpImage=srcImage;//将原始图赋给临时变量
float up=6;

resize(tmpImage,dstImage1,Size(tmpImage.cols/up,tmpImage.rows/up),(0,0),(0,0),3);
resize(tmpImage,dstImage2,Size(tmpImage.cols*up,tmpImage.rows*up),(0,0),(0,0),3);

imwrite("D:\\2.bmp",dstImage1);
imwrite("D:\\3.bmp",dstImage2);

int height, width;
CRect rect;//定义矩形类
CRect rect1;
CImage image; //创建图片类
CString imgPath;
imgPath="D:\\3.bmp";
image.Load(imgPath);
height = image.GetHeight();
width = image.GetWidth();

m_pic.GetClientRect(&rect); //获得pictrue控件所在的矩形区域
CDC *pDc = m_pic.GetDC();//获得pictrue控件的Dc
SetStretchBltMode(pDc->m_hDC,STRETCH_HALFTONE);

if(width<=rect.Width() && height<=rect.Width()) //小图片,不缩放
{
rect1 = CRect(rect.TopLeft(), CSize(width,height));
image.StretchBlt(pDc->m_hDC,rect1,SRCCOPY); //将图片画到Picture控件表示的矩形区域
}
else
{
float xScale=(float)rect.Width()/(float)width;
float yScale=(float)rect.Height()/(float)height;
float ScaleIndex=(xScale>=yScale?xScale:yScale);
rect1 = CRect(rect.TopLeft(), CSize((int)width*ScaleIndex,(int)height*ScaleIndex));
image.StretchBlt(pDc->m_hDC,rect1,SRCCOPY); //将图片画到Picture控件表示的矩形区域
}
ReleaseDC(pDc);//释放picture控件的Dc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mfc