基于VC.NET的GDI+图像处理(3)
图像旋转和拉伸
图像的旋转和拉伸通常是通过在DrawImage中指定destPoints参数来实现,destPoints包含对新的坐标系定义的点的数据。图7.18说明了坐标系定义的方法。
从图中可以看出,destPoints中的第一个点是用来定义坐标原点的,第二点用来定义X轴的方法和图像X方向的大小,第三个是用来定义Y轴的方法和图像Y方向的大小。若destPoints定义的新坐标系中两轴方向不垂直,就能达到图像拉伸的效果。
下面的代码就是图像旋转和拉伸的一个示例,其结果如图7.19所示。
Image image(L"sunflower.jpg");
graphics.DrawImage(&image, 10,10);
Point points[] = { Point(0, 0), Point(image.GetWidth(), 0),
Point(0, image.GetHeight())};
Matrix matrix(1,0,0,1,230,10); // 定义一个单位矩阵,坐标原点在(230,10)
matrix.Rotate(30); // 顺时针旋转30度
matrix.Scale(0.63,0.6); // X 和 Y 方向分别乘以0.63和0.6比例因子
matrix.TransformPoints(points, 3); // 用该矩阵转换points
graphics.DrawImage(&image, points, 3);
Point newpoints[] = {Point(450, 10), Point(510, 60), Point(350, 80)};
graphics.DrawImage(&image, newpoints, 3);
当然,对于图像旋转还可直接使用Graphics::RotateTransform来进行,例如下面的代码。但这样设置后,以后所有的绘图结果均会旋转,有时可能感觉不方便。
Image image(L"sunflower.jpg");
graphics.TranslateTransform(230,10); // 将原点移动到(230,10)
graphics.RotateTransform(30); // 顺时针旋转30度
graphics.DrawImage(&image, 0,0);
调整插补算法的质量
当图像进行缩放时,需要对图像像素进行插补,不同的插补算法其效果是不一样的。Graphics:: SetInterpolationMode可以让我们根据自己的需要使用不同质量效果的插补算法。当然,质量越高,其渲染时间越长。下面的代码就是使用不同质量效果的插补算法模式,其结果如图7.20所示。 Graphics graphics( pDC->m_hDC );
Image image(L"log.gif");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
// 不进行缩放
graphics.DrawImage( &image,10,10);
// 使用低质量的插补算法
graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);
graphics.DrawImage( &image,
Rect(170, 30, (INT)(0.6*width), (INT)(0.6*height)));
// 使用中等质量的插补算法
graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear);
graphics.DrawImage( &image,
Rect(270, 30, (INT)(0.6*width), (INT)(0.6*height)));
// 使用高质量的插补算法
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
graphics.DrawImage( &image,
Rect(370, 30, (INT)(0.6*width), (INT)(0.6*height)));
事实上,Image功能还不止这些,例如还有不同格式文件之间的转换等。但这些功能和MFC的新类CImage功能基本一样,但CImage更符合MFC程序员的编程习惯,因此下一节中我们来重点介绍CImage的使用方法和技巧。
- 上一篇:清理VC工程
- 下一篇:深入浅出VC++串口编程之短信应用开发