为可以调整大小和居中定位的字符串创建矩形
Create rectangle for the string that can be resized and center positioned
起初,我使用drawString和GraphicsPath.AddString在pictureBox中绘制outlined/solid文字。我可以更改它的字体大小、样式和字体系列,但我意识到我无法 resized/stretch 文本,因为字体大小按比例分配给字符串。所以我被告知的解决方案是这样的:
I have been advised that in order to scale a Text (from a Draw String), I need to use a rectangle on which the text will depend on. In that way, I can resize the whole text (width, height, both). But I have no idea how to do it.
PS。如果还有其他方法,你可以告诉我。谢谢大家
这是我的 TextDrawing 方法:
public void DrawRects(Font f, string text, Graphics g, RectangleF rect)
{
List<RectangleF> list = new List<RectangleF>();
using (StringFormat format = new StringFormat())
{
int i;
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center;
format.Trimming = StringTrimming.None;
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
CharacterRange[] ranges = new CharacterRange[text.Length];
for (i = 0; i < text.Length; i++)
{
ranges[i] = new CharacterRange(i, 1);
}
format.SetMeasurableCharacterRanges(ranges);
Region[] regionArray = g.MeasureCharacterRanges(text, f, rect, format);
for (i = 0; i < regionArray.Length; i++)
{
list.Add(regionArray[i].GetBounds(g));
}
foreach (RectangleF r in list)
{
//g.SmoothingMode = SmoothingMode.AntiAlias;
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
//g.InterpolationMode = InterpolationMode.High;
g.DrawRectangle(Pens.LightBlue, Rectangle.Round(r));
}
using (GraphicsPath path = new GraphicsPath())
{
path.AddString(text, f.FontFamily, Convert.ToInt32(f.Style), g.DpiY * rect.Height/72f, rect.Location, format);
RectangleF text_rectf = path.GetBounds();
PointF[] target_pts = {
new PointF(rect.Left, rect.Top),
new PointF(rect.Right, rect.Top),
new PointF(rect.Left, rect.Bottom)};
g.Transform = new Matrix(text_rectf, target_pts);
g.FillPath(Brushes.Red, path);
g.DrawPath(Pens.Red, path);
g.ResetTransform();
}
//g.SmoothingMode = SmoothingMode.AntiAlias;
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
//g.InterpolationMode = InterpolationMode.High;
//g.DrawString(text, f, Brushes.Red, rect, format);
}
}
还有我的UI供大家参考:
我需要的结果:
编辑: 我更改了文本绘图上的代码,但我仍然不能做的是在每个字母上创建不同的矩形,这些矩形可以使用轨迹栏调整大小。
我试了一下,但无法计算出矩形与字母的关系...
尽管如此,我提出了一个更优雅、经过时间考验且数学上正确的解决方案。
Alex Fr 在他的 DrawTools 文章 中提供了一组出色的绘图工具,并且该项目是 Draw Tool Redux.
的基础
Alex Fr 的原始项目基于 Microsoft C++ MFC 示例项目,开发人员可以从 DRAWCLI 中学习。 DrawTools C# 程序重现了一些 DRAWCLI 功能并使用了此示例中的一些设计决策。这些天看到它的唯一方法是通过 WayBack machine link: https://web.archive.org/web/20120814082327/https://www.codeproject.com/Articles/8494/DrawTools
我建议您交换绘图库,并从设计精良的解决方案着手。 Draw Tool Redux 具有我认为您需要的大部分功能。除了旋转偏移,我相信我已经看到 example of in Rod Stephens book, here it is on WayBack Machine again: Interactively rotate images by an arbitrary angle in C#.
起初,我使用drawString和GraphicsPath.AddString在pictureBox中绘制outlined/solid文字。我可以更改它的字体大小、样式和字体系列,但我意识到我无法 resized/stretch 文本,因为字体大小按比例分配给字符串。所以我被告知的解决方案是这样的:
I have been advised that in order to scale a Text (from a Draw String), I need to use a rectangle on which the text will depend on. In that way, I can resize the whole text (width, height, both). But I have no idea how to do it.
PS。如果还有其他方法,你可以告诉我。谢谢大家
这是我的 TextDrawing 方法:
public void DrawRects(Font f, string text, Graphics g, RectangleF rect)
{
List<RectangleF> list = new List<RectangleF>();
using (StringFormat format = new StringFormat())
{
int i;
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center;
format.Trimming = StringTrimming.None;
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
CharacterRange[] ranges = new CharacterRange[text.Length];
for (i = 0; i < text.Length; i++)
{
ranges[i] = new CharacterRange(i, 1);
}
format.SetMeasurableCharacterRanges(ranges);
Region[] regionArray = g.MeasureCharacterRanges(text, f, rect, format);
for (i = 0; i < regionArray.Length; i++)
{
list.Add(regionArray[i].GetBounds(g));
}
foreach (RectangleF r in list)
{
//g.SmoothingMode = SmoothingMode.AntiAlias;
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
//g.InterpolationMode = InterpolationMode.High;
g.DrawRectangle(Pens.LightBlue, Rectangle.Round(r));
}
using (GraphicsPath path = new GraphicsPath())
{
path.AddString(text, f.FontFamily, Convert.ToInt32(f.Style), g.DpiY * rect.Height/72f, rect.Location, format);
RectangleF text_rectf = path.GetBounds();
PointF[] target_pts = {
new PointF(rect.Left, rect.Top),
new PointF(rect.Right, rect.Top),
new PointF(rect.Left, rect.Bottom)};
g.Transform = new Matrix(text_rectf, target_pts);
g.FillPath(Brushes.Red, path);
g.DrawPath(Pens.Red, path);
g.ResetTransform();
}
//g.SmoothingMode = SmoothingMode.AntiAlias;
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
//g.InterpolationMode = InterpolationMode.High;
//g.DrawString(text, f, Brushes.Red, rect, format);
}
}
还有我的UI供大家参考:
我需要的结果:
编辑: 我更改了文本绘图上的代码,但我仍然不能做的是在每个字母上创建不同的矩形,这些矩形可以使用轨迹栏调整大小。
我试了一下,但无法计算出矩形与字母的关系...
尽管如此,我提出了一个更优雅、经过时间考验且数学上正确的解决方案。
Alex Fr 在他的 DrawTools 文章 中提供了一组出色的绘图工具,并且该项目是 Draw Tool Redux.
的基础Alex Fr 的原始项目基于 Microsoft C++ MFC 示例项目,开发人员可以从 DRAWCLI 中学习。 DrawTools C# 程序重现了一些 DRAWCLI 功能并使用了此示例中的一些设计决策。这些天看到它的唯一方法是通过 WayBack machine link: https://web.archive.org/web/20120814082327/https://www.codeproject.com/Articles/8494/DrawTools
我建议您交换绘图库,并从设计精良的解决方案着手。 Draw Tool Redux 具有我认为您需要的大部分功能。除了旋转偏移,我相信我已经看到 example of in Rod Stephens book, here it is on WayBack Machine again: Interactively rotate images by an arbitrary angle in C#.