GDI+旋转图片的几种方法

来源:互联网 发布:股票分析系统源码 编辑:程序博客网 时间:2024/06/09 22:47
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恢复矩阵后, 再绘制其它
1 0