我正在使用 GDI+ 绘制一些带有黑色轮廓的白色文本。
我尝试使用图形路径,但效果不佳(尤其是使用小文本),因此我考虑在文本周围 8 个像素的位置渲染黑色文本,然后在顶部绘制白色。
结果是我想要的那种东西,但代码似乎并不那么有效。有没有更好的方法来达到同样的结果?
我的代码:
Private _whiteFont As New Font("Segoe UI", 8)
Private _blackFont As New Font("Segoe UI", 8)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DrawTextWithOutline("12", New Point(18, 9))
End Sub
Private Sub DrawTextWithOutline(ByVal text As String, ByVal pt As Point)
Using g As Graphics = Me.CreateGraphics
g.DrawString(text, _blackFont, Brushes.Black, pt.X - 1, pt.Y) 'left
g.DrawString(text, _blackFont, Brushes.Black, pt.X, pt.Y + 1) 'top
g.DrawString(text, _blackFont, Brushes.Black, pt.X + 1, pt.Y) 'right
g.DrawString(text, _blackFont, Brushes.Black, pt.X, pt.Y + 1) 'bottom
g.DrawString(text, _blackFont, Brushes.Black, pt.X - 1, pt.Y - 1) 'top left
g.DrawString(text, _blackFont, Brushes.Black, pt.X - 1, pt.Y + 1) 'bottom left
g.DrawString(text, _blackFont, Brushes.Black, pt.X + 1, pt.Y - 1) 'top right
g.DrawString(text, _blackFont, Brushes.Black, pt.X + 1, pt.Y + 1) 'bottom right
g.DrawString(text, _whiteFont, Brushes.White, pt)
End Using
End Sub
样本结果: