You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET WinForms的PrintPreviewDialog中渲染扩展Unicode字符

问题原因

PrintPreviewDialog 和 PrintDocument 的 PrintPage 事件中使用 e.Graphics.DrawString 基于 GDI+ 渲染,和窗体按钮使用的 GDI 文本渲染逻辑不同:GDI 会自动触发字体回退机制,当指定字体不支持目标字符时,自动调用系统中支持该字符的字体完成渲染;而 GDI+ 的 DrawString 不会自动做字体回退,Microsoft Sans Serif 本身不支持 U+1F87A 这个补充平面的 Unicode 字符,就会渲染为方框。

解决方法

方法1:直接指定支持该字符的字体

Windows 系统自带的 Segoe UI Symbol 字体原生支持 U+1F87A 箭头字符,直接替换字体即可:

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles Doc.PrintPage
    Dim arrow As String = Char.ConvertFromUtf32(&H1F87A)
    ' 替换为支持该字符的字体
    Dim f As New System.Drawing.Font("Segoe UI Symbol", 11, FontStyle.Regular)
    e.Graphics.DrawString(arrow, f, Brushes.Black, 100, 100)
End Sub

方法2:使用 TextRenderer 渲染文本(更通用)

TextRenderer.DrawText 使用和窗体控件完全一致的 GDI 渲染逻辑,自动处理字体回退,不需要修改指定字体,兼容性更好:

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles Doc.PrintPage
    Dim arrow As String = Char.ConvertFromUtf32(&H1F87A)
    Dim f As New System.Drawing.Font("Microsoft Sans Serif", 11, FontStyle.Regular)
    ' 替换DrawString为TextRenderer.DrawText
    TextRenderer.DrawText(e.Graphics, arrow, f, New Point(100, 100), Color.Black)
End Sub

如果需要调整文本渲染质量,可以提前设置 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit 优化显示效果。

内容的提问来源于stack exchange,提问作者D_Bester

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 23:15:04