gpt4 book ai didi

.net - 我可以改进这段代码来绘制轮廓文本吗

转载 作者:行者123 更新时间:2023-12-03 03:43:54 25 4
gpt4 key购买 nike

我正在使用 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

结果示例: enter image description here

最佳答案

您没有发布您的 GraphicsPath 尝试,但此版本似乎绘制正常。我使用粗体字体来尝试在内部获得更多空白,并使用 2 像素黑笔来获取轮廓:

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
Using gp As New GraphicsPath, _
f As New Font("Segoe UI", 8, FontStyle.Bold), _
p As New Pen(Brushes.Black, 2)
gp.AddString(text, f.FontFamily, f.Style, f.Size + 3, New Point(100, 40), _
StringFormat.GenericTypographic)
e.Graphics.DrawPath(p, gp)
e.Graphics.FillPath(Brushes.White, gp)
End Using

情人眼里出西施:

enter image description here

关于.net - 我可以改进这段代码来绘制轮廓文本吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429389/

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