【问题标题】:Draw outline around string without artifacts in .NET在 .NET 中在没有伪影的情况下围绕字符串绘制轮廓
【发布时间】:2014-08-21 18:49:16
【问题描述】:

我写了一个脚本,在 Powershell 中将文本渲染成图像。

这些应该是相关的代码:

$fontFamily = new-object System.Drawing.FontFamily "Arial"
$stringFormat = new-object System.Drawing.StringFormat
$fontSize = 50

# background brush, transparent
$brushBg = [System.Drawing.Brushes]::Black
# foreground brush
$colorFg = [System.Drawing.Color]::FromArgb(105, 160, 215)
$colorOutline = [System.Drawing.Color]::FromArgb(255, 255, 255)
$brushFg = new-object System.Drawing.SolidBrush($colorFg)

$path = new-object System.Drawing.Drawing2D.GraphicsPath
$path.AddString("sometext", $fontFamily, [int]([System.Windows.FontStyle]::Regular), ([float] $fontSize), (new-object System.Drawing.Point 0, 0), $stringFormat)

# ...
# magically get some graphics context to draw on
# ....

# draw the outline in white
$pen = new-object System.Drawing.Pen($colorOutline, 12)
$graphics.DrawPath($pen, $path)
$pen.Dispose()

# draw the text itself in blue
$graphics.FillPath($brushFg, $path)

...

现在的问题是,当我使轮廓太宽时,我会从 DrawPath 中得到难看的伪影,如下图所示。

如何避免它们?

桑德罗

【问题讨论】:

  • 这完全有道理,就像一个魅力!如果您将其发布为答案,我很高兴为您提供信誉:-)

标签: .net graphics drawing


【解决方案1】:

这些尖峰是由 W 形的急剧过渡引起的。笔的 MiterLimit 的副作用。更改 Pen.LineJoin 属性通常是最好的,您可以通过选择 Bevel 或 Round 来消除这些斜接伪影。

【讨论】:

  • 像魅力一样工作!谢谢! :-)
猜你喜欢
  • 1970-01-01
  • 2016-11-05
  • 2016-03-14
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 2019-11-24
相关资源
最近更新 更多