我要在面板上绘制以下文本:
这是一种彩色文本。
我发现此article与绘制彩色文本有关。
我用单词替换了字符,但是没有用。
(我使用FillPath / DrawPath绘制文本)
我的代码:
private void Form1_Paint(object sender, PaintEventArgs e)
{
const string txt = "C# Helper! Draw some text with each letter in a random color.";
// Make the font.
using (Font the_font = new Font("Times New Roman", 40,
FontStyle.Bold | FontStyle.Italic))
{
// Make a StringFormat object to use for text layout.
using (StringFormat string_format = new StringFormat())
{
// Center the text.
string_format.Alignment = StringAlignment.Center;
string_format.LineAlignment = StringAlignment.Center;
string_format.FormatFlags = StringFormatFlags.NoClip;
// Make CharacterRanges to indicate which
// ranges we want to measure.
MatchCollection mc = Regex.Matches(txt, @"[^\s]+");
CharacterRange[] ranges = new CharacterRange[mc.Count];
int g = 0;
foreach (Match m in mc)
{
ranges[g] = new CharacterRange(m.Index, m.Length);
g++;
}
string_format.SetMeasurableCharacterRanges(ranges);
// Measure the text to see where each character range goes.
Region[] regions =
e.Graphics.MeasureCharacterRanges(
txt, the_font, this.ClientRectangle,
string_format);
// Draw the characters one at a time.
for (int i = 0; i < ranges.Length; i++)
{
// See where this character would be drawn.
RectangleF rectf = regions[i].GetBounds(e.Graphics);
Rectangle rect = new Rectangle(
(int)rectf.X, (int)rectf.Y,
(int)rectf.Width, (int)rectf.Height);
// Make a brush with a random color.
using (Brush the_brush = new SolidBrush(RandomColor()))
{
// Draw the character.
string txts = txt.Substring(ranges[i].First, ranges[i].Length);
e.Graphics.DrawString(txts,
the_font, the_brush, rectf, string_format);
}
}
}
}
}
出什么问题了?
答案 0 :(得分:0)
(在某种程度上)这是经典。
MeasureCharacterRanges执行的非常精确的度量与Graphics.DrawString
执行的实际字符串绘制之间存在差异。
由RectagleF
返回的Region.GetBounds()
考虑文本的大小。
另一方面,Graphics.DrawString
在计算给定范围内的Text布置时执行某种网格拟合。
我不会在这里解释它,这是一个很广泛的问题,但是我已经写了一些关于它的内容:
Drawing a Long String on to a Bitmap results in Drawing Issues。
如果您有兴趣,可以在此上下文中找到有关Graphics
对象行为的一些详细信息。
总和是,正确测量了文本,但是Graphics.DrawString
执行的调整导致文本无法完全适合所测量的边界:绘制的文本略微溢出 。
您可以使用几个StringFormat
标志来纠正此问题:
添加[StringFormat].Trimming = StringTrimming.None
应用此设置后,您可以立即看到问题所在:最后一个字符(或几个字符)被包装到新行中,使图形混乱。
要更正它,请将StringFormatFlags.NoWrap
添加到StringFormatFlags.NoClip
显然,这将解决问题。显然是因为现在整个字符串都画在一行上。
我为您建议了另一种方法,使用TextRenderer.DrawText渲染字符串。
请注意,TextRenderer
实际上是WinForms
控件(不是所有控件)用来将Text呈现到屏幕上的类。
这是使用以下方法的结果:
示例代码,使用原始代码进行一些修改:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Control control = sender as Control;
const string txt = "C# Helper! Draw some text with each word in a random color.";
TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter |
TextFormatFlags.NoPadding | TextFormatFlags.NoClipping;
using (StringFormat format = new StringFormat())
{
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
MatchCollection mc = Regex.Matches(txt, @"[^\s]+");
CharacterRange[] ranges = mc.Cast<Match>().Select(m => new CharacterRange(m.Index, m.Length)).ToArray();
format.SetMeasurableCharacterRanges(ranges);
using (Font font = new Font("Times New Roman", 40, FontStyle.Regular, GraphicsUnit.Point))
{
Region[] regions = e.Graphics.MeasureCharacterRanges(txt, font, control.ClientRectangle, format);
for (int i = 0; i < ranges.Length; i++)
{
Rectangle WordBounds = Rectangle.Round(regions[i].GetBounds(e.Graphics));
string word = txt.Substring(ranges[i].First, ranges[i].Length);
TextRenderer.DrawText(e.Graphics, word, font, WordBounds, RandomColor(), flags);
}
}
}
}
private Random rand = new Random();
private Color[] colors =
{
Color.Red,
Color.Green,
Color.Blue,
Color.Lime,
Color.Orange,
Color.Fuchsia,
};
private Color RandomColor()
{
return colors[rand.Next(0, colors.Length)];
}