0

好的,所以我正在尝试为我的程序使用 privatefontcollection 来为其添加一点独特性。默认情况下,我使用的字体未安装在计算机上。字体的名称是 youmurdererbb_reg。我在资源文件夹中有字体文件,该文件采用 .ttf 格式。这是我到目前为止所拥有的:

Imports System.Drawing
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Text

Dim pc As New PrivateFontCollection


    Private Sub Main_Menu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Try
            pc.AddFontFile(YouMurderer)
        Catch ex As Exception
            Trace.WriteLine(ex.ToString)
        End Try

    End Sub


    Private Sub Main_Menu_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim Fnt As Font = New Font(pc.Families(0), 80, FontStyle.Regular)
    e.Graphics.DrawString("This is the text that is being drawn", Fnt, Brushes.Black, 10, 10)

    End Sub

现在我声明了私人字体集合,我尝试了这些不同的方法来让它工作:

Dim YouMurderer As String = Encoding.ASCII.GetString(My.Resources.youmurdererbb_reg)

Dim YouMurderer As String = Convert.ToString(My.Resources.youmurdererbb_reg)

Dim YouMurderer As String = Convert.ToBase64String(My.Resources.youmurdererbb_reg)

Dim YouMurderer As String = Encoding.UTF8.GetString(My.Resources.youmurdererbb_reg)

但无论我选择哪一个,它都会用一个大的红色“X”显示整个表单(就像一个图片框的“ErrorImage”)(我有一张图片设置为表单背景作为附加信息)。

另一个问题是,如果我不尝试转换它:

Dim YouMurderer As String = My.Resources.youmurdererbb_reg

然后它出现了以下错误:

Value of type '1-dimensional array of Byte' cannot be converted to 'String'.

我需要 .NET(框架 4)中的帮助!整个程序是用 VB.net 编写的,而不是 C#、C++ 或 JAVA。

4

1 回答 1

0

我创建了一个名为BizArk的库,使这变得容易。您可以使用NuGet安装它,或者,如果您只想使用源代码作为参考,您可以在此处获取代码(Current/BizArkCore/Util/FontUtil.cs)。请注意,代码是用 C# 编写的,但代码并不多,所以希望您能够跟进。

如果您按原样使用 FontUtil 类,则可以使用它来创建任何字体,甚至是内置字体。以下是如何使用它...

Private Sub Main_Menu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Try
        FontUtil.RegisterFont(My.Resources.youmurdererbb_reg)
    Catch ex As Exception
        Trace.WriteLine(ex.ToString)
    End Try

End Sub

Private Sub Main_Menu_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim Fnt As Font = FontUtil.Create("YouMurderer", 80, FontStyle.Regular)
    e.Graphics.DrawString("This is the text that is being drawn", Fnt, Brushes.Black, 10, 10)
    Fnt.Dispose()

End Sub
于 2012-02-10T19:18:15.303 回答