GDI+图像的显示、裁剪、缩放、转置…

在vs2017对话框模式下测试通过
using namespace Gdiplus;
void CXXDlg::OnPaint()
{
//下面两句要加上,不然无法显示图片,不知道为什么
CImage image1;
image1.Load(L"");
// 图像的显示
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Image image(L"pic.jpg");

 graph.DrawImage(&image, 70, 50);
 ReleaseDC(pDC);

 // 使用负坐标显示图像
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Image image(L"pic.jpg");

 graph.DrawImage(&image, -70, -50);
 ReleaseDC(pDC);

 // 不同分辨率图片的显示情况
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Image image(L"pic.jpg");

 graph.DrawImage(&image, 0, 0); // 绘制图像

 CString strPrint;    // 输出的字符串
 double f = graph.GetDpiX() / image.GetHorizontalResolution(); // 缩放比例

 strPrint.Format(L"屏幕分辨率是%.0f, 图像分辨率是%.0f, 缩放比例是%.1f",
  graph.GetDpiX(), image.GetHorizontalResolution(), f);
 graph.DrawString(strPrint, -1, &Font(L"黑体", 17, FontStyleBold),
  PointF(0, 280), &SolidBrush(Color::Red));

 ReleaseDC(pDC);

 // 图像的缩放方法1
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());

 CRect winRect;
 GetClientRect(winRect);  // 获得窗口绘制区的大小

 Image image(L"pic.jpg");
 graph.DrawImage(&image, 0, 0,  winRect.Width(), winRect.Height()); // 绘制图像

 ReleaseDC(pDC);
 
 // 图像的缩放方法2
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());

 CRect winRect;
 GetClientRect(winRect);  // 获得窗口绘制区的大小

 Rect rect(0, 0, winRect.Width(), winRect.Height()); // 创建绘制区域的矩形对象

 Image image(L"pic.jpg");
 graph.DrawImage(&image, rect); // 绘制图像

 ReleaseDC(pDC);

 ****************************************************************************
          6.4.4节代码
 ****************************************************************************

 // 图像的剪裁显示
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Image image(L"pic.jpg");

 // 从原图片的(12, 80)开始,剪裁长为430,宽为180的矩形区域
 // 然后将其显示在(80, 90)处
 graph.DrawImage(&image, 80, 90, 12, 80, 430, 180, UnitPixel);

 ReleaseDC(pDC);

 // 剪裁并缩放图像
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Image image(L"pic.jpg");
 int nSrcWidth = 430; // 剪裁区域的宽度
 int nSrcHeight = 180; // 剪裁区域的高度
 // 构造显示的矩形区域
 Rect destRect(15, 40, (int)(nSrcWidth*1.2), (int)(nSrcHeight*1.2));

 graph.DrawImage(&image, destRect, 12, 80, nSrcWidth, nSrcHeight, UnitPixel);

 ReleaseDC(pDC);

 // 图像的转置
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Bitmap image(L"pic.jpg");

 
 // 输出原始图像
 graph.DrawImage(&image, 0, 0);

 // 输出转置图像的坐标
 int nX = 500;
 int nY = 0;

 // 图像长宽
 int nWidth = image.GetWidth();
 int nHeight = image.GetHeight();

 Point points[] = {     // 输出位置需要加上转置图像的坐标
  Point(nX, nY),      // 原图像左上角的输出位置
  Point(nX, nY + nWidth),    // 原图像右上角的输出位置
  Point(nX + nHeight, nY) };   // 原图像左下角的输出位置

 graph.DrawImage(&image, points, 3);

 ReleaseDC(pDC);

 // 图像的镜像
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Bitmap image(L"pic.jpg");
 
 // 图像长宽
 int nWidth = image.GetWidth();
 int nHeight = image.GetHeight();

 // 水平镜像的输出位置
 int nX = 0;
 int nY = 0;

 Point HoPoints[] = {
   Point(nX + nWidth, nY),   
   Point(nX, nY),     
   Point(nX + nWidth, nY + nHeight) };

 graph.DrawImage(&image, HoPoints, 3);   // 输出水平镜像

 // 垂直镜像的输出位置
 nX = nWidth + 5;
 nY = 0;

 Point VePoints[] = {
  Point(nX, nY + nHeight),   
  Point(nX + nWidth, nY + nHeight),     
  Point(nX, nY) };

 graph.DrawImage(&image, VePoints, 3);   // 输出垂直镜像

 ReleaseDC(pDC);
 
 // 图像的简单旋转
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Bitmap image(L"pic.jpg");
 
 // 图像长宽
 int nWidth = image.GetWidth();
 int nHeight = image.GetHeight();

 // 旋转90度图像的输出位置
 int nX = 0;
 int nY = 0;

 Point points90[] = {
   Point(nX + nHeight, nY),
   Point(nX + nHeight, nY + nWidth),
   Point(nX, nY) };

 graph.DrawImage(&image, points90, 3); // 输出旋转了90度的图像

 // 旋转180度图像的输出位置
 nX = nHeight + 5;
 nY = 0;

 Point points180[] = {
  Point(nX + nWidth, nY + nHeight),
  Point(nX, nY + nHeight),
  Point(nX + nWidth, nY) };

 graph.DrawImage(&image, points180, 3); // 输出旋转了180度的图像

 ReleaseDC(pDC);

 // 图像的变形
 
 CDC* pDC = GetDC();
 Graphics graph(pDC->GetSafeHdc());
 Bitmap image(L"pic.jpg");
 
 // 图像长宽
 int nWidth = image.GetWidth();
 int nHeight = image.GetHeight();

 // 变形图像1的输出位置
 int nX = 0;
 int nY = 0;

 Point points1[] = {
   Point(nX, nY),
   Point(nX + nWidth, nY + 0),
   Point(nX + 60, nY + nHeight) };

 graph.DrawImage(&image, points1, 3); // 输出变形图形1

 // 变形图像2的输出位置
 nX = 520;
 nY = 0;

 Point points2[] = {
  Point(nX + 52, nY),
  Point(nX + 374, nY + 200),
  Point(nX, nY + 117) };

 graph.DrawImage(&image, points2, 3); // 输出变形图形2
}
发布了117 篇原创文章 · 获赞 4 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36266449/article/details/78194672
今日推荐