使用 Drawstring 垂直翻转文本
Flip text vertically using Drawstring
我有一些代码可以将一些文本写入定义的区域。
graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat);
在某些情况下,我想水平翻转文本,使其来自:
到
我已经尝试测量字符串宽度并取其倒数:
float w = graphics.MeasureString(text, goodFont).Width;
graphics.DrawString(text, goodFont, Brushes.Black, -w, 0, stringFormat);
但我的问题是文本超出了我希望在 (textarea) 中绘制的框的边界。
我想水平翻转文本,同时保持我的框边界。谁能指出我完成任务的正确方向?
提前致谢!
编辑:我试图避免创建位图然后进行转换。
你可以使用 Transformation Matrix
类似于:
float w = graphics.MeasureString(text, goodFont).Width;
graphics.MultiplyTransform(new Matrix(-1, 0, 0, 1, w, 0));
/*
Matrix:
-1 0
0 1
newX -> -x
newY -> y
and dx offset = w (since we need to move image to right because of new negative x)
*/
graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat);
graphics.ResetTransform();
您可能需要使用 Matrix/area 参数,因为我正在盲目编码,但我希望您明白了。
您可以使用图形变换。我看到的更简单的方法是像这样使用 this Matrix Constructor (Rectangle, Point[]):
Point[] transformPoints =
{
// upper-left:
new Point(textarea.Right - 1, textarea.Top),
// upper-right:
new Point(textarea.Left + 1, textarea.Top),
// lower-left:
new Point(textarea.Right - 1, textarea.Bottom),
};
var oldMatrix = graphics.Transform;
var matrix = new Matrix(textarea, transformPoints);
try
{
graphics.Transform = matrix;
graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat);
}
finally
{
graphics.Transform = oldMatrix;
matrix.Dispose();
}
P.S。尽管@serhiyb 在我之前几秒钟发布了类似的答案,但我认为这更容易理解 - 您只需指定源矩形以及如何转换其左上角、右上角和左下角点即可定义转换。
您可以使用Matrix Constructor
to transform the graphics and later draw the graphics using the DrawString
方法。
试试这个:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
string text = "This is a Test";
g.DrawString(text, Font, Brushes.Black, 0, 0);
g.MultiplyTransform(new Matrix(-1, 0, 0, 1, 68, 50));
g.DrawString(text, Font, Brushes.Black, 0, 0);
g.ResetTransform();
}
输出:
我有一些代码可以将一些文本写入定义的区域。
graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat);
在某些情况下,我想水平翻转文本,使其来自:
到
我已经尝试测量字符串宽度并取其倒数:
float w = graphics.MeasureString(text, goodFont).Width;
graphics.DrawString(text, goodFont, Brushes.Black, -w, 0, stringFormat);
但我的问题是文本超出了我希望在 (textarea) 中绘制的框的边界。
我想水平翻转文本,同时保持我的框边界。谁能指出我完成任务的正确方向?
提前致谢!
编辑:我试图避免创建位图然后进行转换。
你可以使用 Transformation Matrix
类似于:
float w = graphics.MeasureString(text, goodFont).Width;
graphics.MultiplyTransform(new Matrix(-1, 0, 0, 1, w, 0));
/*
Matrix:
-1 0
0 1
newX -> -x
newY -> y
and dx offset = w (since we need to move image to right because of new negative x)
*/
graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat);
graphics.ResetTransform();
您可能需要使用 Matrix/area 参数,因为我正在盲目编码,但我希望您明白了。
您可以使用图形变换。我看到的更简单的方法是像这样使用 this Matrix Constructor (Rectangle, Point[]):
Point[] transformPoints =
{
// upper-left:
new Point(textarea.Right - 1, textarea.Top),
// upper-right:
new Point(textarea.Left + 1, textarea.Top),
// lower-left:
new Point(textarea.Right - 1, textarea.Bottom),
};
var oldMatrix = graphics.Transform;
var matrix = new Matrix(textarea, transformPoints);
try
{
graphics.Transform = matrix;
graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat);
}
finally
{
graphics.Transform = oldMatrix;
matrix.Dispose();
}
P.S。尽管@serhiyb 在我之前几秒钟发布了类似的答案,但我认为这更容易理解 - 您只需指定源矩形以及如何转换其左上角、右上角和左下角点即可定义转换。
您可以使用Matrix Constructor
to transform the graphics and later draw the graphics using the DrawString
方法。
试试这个:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
string text = "This is a Test";
g.DrawString(text, Font, Brushes.Black, 0, 0);
g.MultiplyTransform(new Matrix(-1, 0, 0, 1, 68, 50));
g.DrawString(text, Font, Brushes.Black, 0, 0);
g.ResetTransform();
}
输出: