您的位置:首页 > 其它

使用GDI+绘制旋转的图形及图片

2008-02-28 14:40 211 查看
如果要绘制半透明的图片,请使用.png格式(用photoshop即可轻松制作并处理)
一、首先介绍Graphics的两个函数,
RotateTransform:将整个坐标系逆时针旋转一定角度
TranslateTransform:将整个坐标系偏移到某个位置
本例要实现的功能是在指定位置上旋转图片,首先需要将整个坐标系偏移到指定位置,在进行坐标系的旋转,在函数使用上应先旋转在偏移(与我们想象的相反),代码如下
myGraphics.RotateTransform(angle,MatrixOrderAppend);
myGraphics.TranslateTransform(pnt.x,pnt.y,MatrixOrderAppend);
将图片画在pnt处,图片的中心点在rect1矩形的中心,DrawImage时坐标为图片在原坐标系的坐标减去坐标系的偏移
myGraphics.DrawImage(pImg,- rect1.Width(),- rect1.Height()/2,rect1.Width(), rect1.Height());
myGraphics.ResetTransform(); //将坐标系复位
二、还有一种方法可实现图形的旋转功能,那就是将所绘制的图形放入GraphicsPath中,然后设置图形变换矩阵,例子如下:
Graphics* g;
GraphicsPath myGraphicsPath(FillModeAlternate);
int offsetrx=m_EllipseData.point1.x -origin.X;
int offsetry=m_EllipseData.point1 .y -origin.Y;
RectF myRectangle(offsetrx,offsetry ,m_EllipseData.point2.x-m_EllipseData.point1.x,m_EllipseData.point2.y-m_EllipseData.point1 .y );

myGraphicsPath.AddEllipse (myRectangle);
Matrix* myPathMatrix=new Matrix();
myPathMatrix->Rotate(nRotateAngle, MatrixOrderAppend);
myPathMatrix->Translate(origin.X,origin.Y,MatrixOrderAppend );
myGraphicsPath.Transform(myPathMatrix);
g->FillPath(&myBrush, &myGraphicsPath);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: