如果你想使用 HTML 来标记你的文本,试试这个:
Private Sub DrawHTMLString(sHTML As String, rct As RectangleF, dpiX As Single, dpiY As Single, g As Graphics)
DrawHTMLString(sHTML, rct.X, rct.Y, rct.Width, rct.Height, dpiX, dpiY, g)
End Sub
Private Sub DrawHTMLString(sHTML As String, x As Single, y As Single, width As Single, height As Single, dpiX As Single, dpiY As Single, g As Graphics)
g.InterpolationMode = InterpolationMode.NearestNeighbor
g.SmoothingMode = SmoothingMode.AntiAlias
g.CompositingQuality = CompositingQuality.AssumeLinear
g.TextRenderingHint = TextRenderingHint.AntiAlias
g.DrawImage(DrawHTMLString(sHTML, width, height, dpiX, dpiY), x, y)
End Sub
Private Function DrawHTMLString(sHTML As String, width As Single, height As Single, dpiX As Single, dpiY As Single) As Bitmap
Dim bmp As Bitmap = Nothing
Dim doc As HtmlDocument = Nothing
Using wb As New WebBrowser()
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate("about:blank")
wb.Width = width : wb.Height = height
doc = wb.Document.OpenNew(True)
doc.Write(sHTML)
bmp = New Bitmap(wb.Width, wb.Height, PixelFormat.Format32bppArgb)
bmp.SetResolution(dpiX, dpiY)
wb.DrawToBitmap(bmp, New Rectangle(0, 0, wb.Width, wb.Height))
End Using
Return bmp
End Function
(对不起,它在 VB.NET 中)
随心所欲地玩它。