【问题标题】:Character style influenced by background while painting it with Graphics.DrawString() (.NET 4.0)使用 Graphics.DrawString() (.NET 4.0) 绘制时受背景影响的字符样式
【发布时间】:2012-07-04 20:11:29
【问题描述】:

背景:我正在使用 WinForms 和 .NET4.0 开发旧版地图应用程序。标签、道路、图标被绘制到一个单独的位图,称为“覆盖”,与地形不同。叠加层的背景是透明的,稍后将在地形位图上绘制。

问题是在位图上绘制文字时,文字样式会受到位图背景的影响。我给你看一个叠加的示例图片:

看看标签“修道院”和“白色”。 “白色”被涂在完全透明的背景上,现在看起来就像是用粗体写的一样。而“abbey”中的“ab”看起来像常规文本,因为它已被绘制在图标上。

这是产生标签的代码:

var bmp = new Bitmap(mapControl.Width, mapControl.Height);
using (var graphics = Graphics.FromImage(bmp))
{
    var labelRectangle = GetLabelBorders();
    Color labelBackgroundColor = Color.FromArgb(128, Color.FromName("LightPink"));
    SolidBrush sb = new SolidBrush(labelBackgroundColor);
    graphics.FillRectangle(sb, labelRectangle );
    graphics.DrawRectangle(WhitePen, labelRectangle .X, labelRectangle .Y, 
                 labelRectangle .Width - 1, labelRectangle .Height - 1);
    Font timesnew8 = new Font("TimesNew", 8);
    StringFormat strForm = new StringFormat();
    strForm.Alignment = StringAlignment.Near;
    graphics.DrawString("abbey", timesnew8, Brushes.Black, labelRectangle , strForm);
}

我希望文字看起来不是粗体。我该怎么做?

【问题讨论】:

  • 您是否尝试过使用平滑模式对文本进行抗锯齿处理?那还是 TextRenderingHint? msdn.microsoft.com/en-us/library/9t6sa8s9.aspxmsdn.microsoft.com/en-us/library/…
  • 你是从哪里画画的?是否启用了双缓冲?
  • @leppie:我更新了代码以更多地反映现实。没有双重缓冲,我正在绘制本地 Bitmap 对象。
  • @user256890:创建后尝试graphics.Clear(labelBackgroundColor)。您可能可以放弃FillRectangle 呼叫。
  • @Tom:TextRenderingHint 设置 System.Drawing.Text.TextRenderingHint.AntiAliasGridFit 效果很好。如果你把它作为一个答案,我会接受它。

标签: .net winforms graphics gdi drawstring


【解决方案1】:

正如前面的评论所说:尝试在 Graphics 对象上使用 TextRenderingHint 属性,看看它是否以不同的方式呈现文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多