UI(16)——GDI+旋转图片的几种方法

1. 使用旋转矩阵

POINT imgRotateCenterPos={10,10}; //旋转中心在图片坐标(相对于图片本身)
CRect rcShow(imgShowRect); //图片要绘制的位置区域.
//把 相对于图片的旋转中心坐标  转换为  绘制区域的坐标
PointF centerPos(imgRotateCenterPos.x+rcShow.left, imgRotateCenterPos.y+rcShow.top);

Matrix mtr;​
//先把源点移动到旋转中心点
mtr.Translate(centerPos.x, centerPos.y);
//旋转一度角度
mtr.Rotate(m_angle);
//还原源点
mtr.Translate(-centerPos.x, -centerPos.y);

//注: 前3个函数的调用, 等价于这一个函数 mtr.RotateAt(angle, centerPos);

//对gp设置变换矩阵
gp.SetTransform(&mtr);
//在某个起点显示图像
gp.DrawImage(pImg, rcShow.left,rcShow.top,rcShow.Width(), rcShow.Height());

2. 直接使用Graphic的方法

POINT imgRotateCenterPos={10,10}; //旋转中心在图片坐标(相对于图片本身)
CRect rcShow(imgShowRect); //图片要绘制的位置区域.
//把 相对于图片的旋转中心坐标  转换为  绘制区域的坐标
PointF centerPos(imgRotateCenterPos.x+rcShow.left, imgRotateCenterPos.y+rcShow.top);

gp.TranslateTransform(centerPos.x,centerPos.y); //源点移动到旋转中心
gp.RotateTransform(m_angle); //旋转
gp.TranslateTransform(-centerPos.x, -centerPos.y);//还原源点

//在某个起点显示图像
gp.DrawImage(pImg, rcShow.left,rcShow.top,rcShow.Width(), rcShow.Height());

3. 使用旋转点

POINT imgRotateCenterPos={10,10}; //旋转中心在图片坐标(相对于图片本身)
CRect rcShow(imgShowRect); //图片要绘制的位置区域.
//把 相对于图片的旋转中心坐标  转换为  绘制区域的坐标
PointF centerPos(imgRotateCenterPos.x+rcShow.left, imgRotateCenterPos.y+rcShow.top);

// 定义一个单位矩阵,坐标原点在表盘中央
  //Matrix matrixH(1,0,0,1,posX,posY);
Matrix matrixH;
  // 时针旋转的角度度,  这里跟前面一样
matrixH.Translate(centerPos.x, centerPos.y);
  matrixH.Rotate(m_angle);
  matrixH.Translate(-centerPos.x,-centerPos.y);

这里的三个点的解释: 这三个点决定了一个平行四边形区域, 这个区域是目标绘图区域.
第一个点是目标区域(left,top), 第二个点(right,top), 第三个点(left,bottom)
Point pointsH[] = { Point(rcShow.left, rcShow.top),Point(rcShow.right, rcShow.top),Point(rcShow.left, rcShow.bottom)};

  // 用该矩阵转换points
  matrixH.TransformPoints( pointsH, 3);
  gp.DrawImage (pImg, pointsH, 3);

如果要在同一个dc中显示多个不同旋转点的图, 需要使用ResetTransform恢复矩阵后, 再绘制其它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值