(VB.net)如何将.ttf(字体)文件用于私有字体集合

时间:2012-02-10 19:07:21

标签: .net fonts drawstring privatefontcollection

好的,所以我试图在我的程序中使用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(Framework 4)中需要帮助!整个程序是用VB.net编写的,而不是C#,C ++或JAVA。

1 个答案:

答案 0 :(得分:0)

我创建了一个名为BizArk的库。您可以使用NuGet安装它,或者,如果您只想使用源作为参考,则可以访问代码here(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