[英]Strange rotation in C# graphics
我在类Class1:Panel中使用OnPaint方法。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
}
旋转并绘制我正在使用的矩形
Matrix m = new Matrix();
m.RotateAt(90, rotationPoint);
g.Transform = m;
g.FillRectangle(Brushes.Black, rectangle)
问题是,旋转无法按照我的意愿进行。
红色正方形是旋转点,位于矩形的中间顶部。 如何设置x,y和旋转点,以便旋转正常工作?
旋转90度后,它应该看起来像这样
红色像素仍位于同一位置。
is not the point, which you want to rotate. 不是要旋转的点。 这是围绕图形旋转的点。 因此,如果您在图形的顶部绘制一个矩形并要旋转它(矩形),则应将旋转点设置为图形的中心,并将图像旋转90度。
这是示例,几乎可以满足您的要求:
base.OnPaint(e);
var g = e.Graphics;
var width = g.VisibleClipBounds.Width;
var height = g.VisibleClipBounds.Height;
var rotationPoint = new PointF(width / 2, height / 2); ;
// draw center point
g.FillRectangle(Brushes.Red, new RectangleF(rotationPoint.X - 5, rotationPoint.Y - 5, 10, 10));
using (var path = new GraphicsPath())
{
var rectangle = new RectangleF((width - 10) / 2, 0, 10, 10);
var m = new Matrix();
m.RotateAt(90, rotationPoint);
path.AddRectangle(rectangle);
path.Transform(m);
// draw rotated point
g.FillPath(Brushes.Black, path);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.