gpt4 book ai didi

c# - 为可以调整大小和居中定位的字符串创建矩形

转载 作者:行者123 更新时间:2023-11-30 17:28:19 24 4
gpt4 key购买 nike

起初,我使用 drawString 和 GraphicsPath.AddString 在 pictureBox 中绘制轮廓/实线文本。我可以更改它的字体大小、样式和字体系列,但我意识到我无法调整/拉伸(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.

附言。如果还有其他方法,你可以告诉我。谢谢大家。

这是我的 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供您引用:

enter image description here

我需要的结果:

enter image description here

编辑:我更改了文本绘图上的代码,我仍然不能做的是在每个字母上创建不同的矩形,这些矩形可以使用轨迹栏调整大小。

最佳答案

我尝试了一下,但我无法计算出矩形与字母的关系...

enter image description here


尽管如此,我提出了一个更优雅、耗时考验且数学上正确的解决方案。

Alex Fr 在他的DrawTools 文章 中提供了一套出色的绘图工具,该项目是Draw Tool Redux 的基础。 .

Alex Fr 的原始项目基于 Microsoft C++ MFC 示例项目,开发人员可以从 DRAWCLI 中学习。 DrawTools C# 程序重现了一些 DRAWCLI 功能并使用了此示例中的一些设计决策。这些天看到它的唯一方法是通过 WayBack 机器链接:https://web.archive.org/web/20120814082327/https://www.codeproject.com/Articles/8494/DrawTools

我建议您交换绘图库,并从设计精良的解决方案着手。 Draw Tool Redux 具有我认为您需要的大部分功能。除了旋转偏移,我相信我已经看到了 example of in Rod Stephens book ,它又出现在 WayBack Machine 上:Interactively rotate images by an arbitrary angle in C# .

关于c# - 为可以调整大小和居中定位的字符串创建矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com