5

我的文本类中有这个方法,我似乎无法翻转整个文本。
我正在使用矩阵来转换GraphicsPath用于绘制字符串的矩阵。

这是我使用@Jimi 回答后的代码:

    public LayerClass DrawString(LayerClass.Type _text, string text, RectangleF rect, Font _fontStyle, Brush brush, float angle, PaintEventArgs e)
    {
        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);
        }
        this._Text = text;
        this._TextRect = rect;
        this.brush = brush;
        this._Angle = angle;
        return new LayerClass(_text, this, text, rect);
    }

现在的问题是,它超出了图片框的中心。

4

1 回答 1

11

有一种更简单的翻转对象Graphics的方法。
创建一个矩阵,它是需要应用于指定对象的所有变换的矩阵相乘的结果。

矩阵变换可以应用于GraphicsPath对象或Graphics对象。或两者兼而有之,当需要按顺序执行多个转换时。

.Net System.Drawing.Drawing2DMatrix 类没有预先构建Flip(镜像)的转换,但是这个 Matrix 结构已经众所周知(我不确定这是否是 Matrix 类中没有特定方法的原因):

| 1 | 0 | 0 |       |-1 | 0 | 0 |       | 1 | 0 | 0 |
| 0 | 1 | 0 |       | 0 | 1 | 0 |       | 0 |-1 | 0 |
| 0 | 0 | 1 |       | 0 | 0 | 1 |       | 0 | 0 | 1 |

   Identity         Mirror X-Axis       Mirror Y-Axis
    Matrix              Matrix             Matrix

您可以注意到(文档中也有报道)第 3 列始终相同,因此,在构建新 Matrix 时,第 3 列值是隐含的,由 Matrix 类初始化提供,因此我们仅指定前 2 列。

重要说明,直接来自 Matrix 类文档:

注意
复合转换的顺序很重要。一般来说,旋转,然后缩放,然后平移与缩放,然后旋转,然后平移是不同的。同样,矩阵乘法的顺序也很重要。一般来说,ABC 与 BAC 不同

Panel使用 GraphicsPath.AddString() 方法在 a 上绘制的字符串示例
两个 Matrix 变换被添加到GraphicsPath对象中:
aFlip-X和 a Flip-Y,它们使用以下Matrix.Multiply()方法组合:

Flip-XFlip-Y矩阵的构建包括和X翻译Y,应用于每个 的第 3 行Matrix。平移值由画布尺寸确定。
例如,Flip-X矩阵:

使用[Canvas].Width = 100 =>
旋转元素:在原点上将 X 轴旋转 180°(-1 弧度)Point(0, 0)
平移元素:将X位置100图形单元向右平移(正值)。

| -1  |  0  |  0  |
|  0  |  1  |  0  |
| 100 |  0  |  1  |

   Mirror X-Axis
  Translate X +100
      Matrix 

效果的视觉表示。
代码中引用的控件与您可以在此处看到的相同(如果您需要重现它)。

矩阵翻转

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

bool flipX = false;
bool flipY = false;
bool outlined = false;
float sampleFontEmSize = 28f;
string sampleText = "Sample Text to Flip";
FontFamily sampleFont = new FontFamily("Segoe UI");

private void panel1_Paint(object sender, PaintEventArgs e)
{
    Panel panel = sender as Panel;
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    using (var path = new GraphicsPath())
    using (var format = new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap))
    {
        format.Alignment = StringAlignment.Center;
        format.LineAlignment = StringAlignment.Center;
        path.AddString(sampleText, sampleFont, (int)FontStyle.Regular, sampleFontEmSize, panel.ClientRectangle, format);

        using (var flipXMatrix = new Matrix(-1, 0, 0, 1, panel.Width, 0))
        using (var flipYMatrix = new Matrix(1, 0, 0, -1, 0, panel.Height))
        using (var transformMatrix = new Matrix())
        {
            if (flipX) {
                transformMatrix.Multiply(flipXMatrix);
            }
            if (flipY) {
                transformMatrix.Multiply(flipYMatrix);
            }
            path.Transform(transformMatrix);
            //Or e.Graphics.Transform = TransformMatrix;

            if (outlined) {
                e.Graphics.DrawPath(Pens.LawnGreen, path);
            }
            else {
                e.Graphics.FillPath(Brushes.Orange, path);
            }
        }
    }
}

private void btnFlipX_Click(object sender, EventArgs e)
{
    flipX = !flipX;
    panel1.Invalidate();
}

private void btnFlipY_Click(object sender, EventArgs e)
{
    flipY = !flipY;
    panel1.Invalidate();
}

private void chkOutlined_CheckedChanged(object sender, EventArgs e)
{
    outlined = chkOutlined.Checked;
    panel1.Invalidate();
}
于 2018-11-07T02:40:18.943 回答