我正在尝试在其中心旋转文本。但是我的代码在旋转时会产生这样的输出。对于 Matrix to RotateAt ,我找不到正确的点:
这是我的代码:
using (StringFormat string_format = new StringFormat())
{
SizeF stringSize = e.Graphics.MeasureString(text, _fontStyle);
rect.Location = new PointF(Shape.center.X - (rect.Width / 2), Shape.center.Y - (rect.Height / 2));
GraphicsContainer gc = e.Graphics.BeginContainer();
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(rect));
RectangleF r = new RectangleF(rect.Location, rect.Size);
GraphicsPath path = new GraphicsPath();
if (text == "" || text == " ")
path.Dispose(); //Disposes the path to prevent OutOfMemoryExcetption
else
{
path.AddString(text, _fontStyle.FontFamily, Convert.ToInt32(_fontStyle.Style), _fontStyle.Height, new Point(0,0), string_format);
RectangleF text_rectf = path.GetBounds();
PointF[] target_pts = {
new PointF(r.Left, r.Top),
new PointF(r.Right, r.Top),
new PointF(r.Left, r.Bottom)};
//e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(r));
using (Matrix m = new Matrix(text_rectf, target_pts))
using (Matrix rotate = new Matrix())
using (Matrix FlipXMatrix = new Matrix(-1, 0, 0, 1, 0, 0))
using (Matrix FlipYMatrix = new Matrix(1, 0, 0, -1, 0, 0))
using (Matrix TransformMatrix = new Matrix())
{
TransformMatrix.Multiply(m);
m.RotateAt(angle, new PointF(0 + (stringSize.Width / 2), +(r.Height * 2)));
rotate.RotateAt(angle, new PointF(r.X, r.Y));
TransformMatrix.Multiply(rotate);
if (flipped)
{
TransformMatrix.Multiply(FlipXMatrix);
}
path.Transform(TransformMatrix);
if (flipY)
{
TransformMatrix.Multiply(FlipYMatrix);
path.Transform(TransformMatrix);
}
//Checks if the user wants the text filled or outlined
if (!isOutlined)
e.Graphics.FillPath(Brushes.Red, path);
else
e.Graphics.DrawPath(Pens.Red, path);
}
}
e.Graphics.EndContainer(gc);
}
PS。绘制矩形仅供参考。
答案 0 :(得分:0)
最后找到了修复程序,或者提出了一种修复方法。该错误是由矩阵的转换引起的。它们应该顺序正确。
using (StringFormat string_format = new StringFormat())
{
SizeF stringSize = e.Graphics.MeasureString(text, _fontStyle);
rect.Location = new PointF(Shape.center.X - (rect.Width / 2), Shape.center.Y - (rect.Height / 2));
GraphicsContainer gc = e.Graphics.BeginContainer();
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(rect));
RectangleF r = new RectangleF(rect.Location, rect.Size);
GraphicsPath path = new GraphicsPath();
if (text == "" || text == " ")
path.Dispose(); //Disposes the path to prevent OutOfMemoryExcetption
else
{
path.AddString(text, _fontStyle.FontFamily, Convert.ToInt32(_fontStyle.Style), _fontStyle.Height, new Point(0,0), string_format);
RectangleF text_rectf = path.GetBounds();
PointF[] target_pts = {
new PointF(r.Left, r.Top),
new PointF(r.Right, r.Top),
new PointF(r.Left, r.Bottom)};
using (Matrix m = new Matrix(text_rectf, target_pts))
using (Matrix rotate = new Matrix())
using (Matrix FlipXMatrix = new Matrix(-1, 0, 0, 1, 500, 0))
using (Matrix FlipYMatrix = new Matrix(1, 0, 0, -1, 0, 500))
using (Matrix TransformMatrix = new Matrix())
{
rotate.RotateAt(angle, new PointF(250,250));
TransformMatrix.Multiply(rotate);
if (flipped)
TransformMatrix.Multiply(FlipXMatrix);
TransformMatrix.Multiply(m);
path.Transform(TransformMatrix);
if (!isOutlined)
e.Graphics.FillPath(Brushes.Red, path);
else
e.Graphics.DrawPath(Pens.Red, path);
}
}
e.Graphics.EndContainer(gc);
}
首先旋转矩阵,然后旋转,然后旋转最终图形。 Matrix.Mulitply 也有所帮助。