簡體   English   中英

(VB.net)如何將.ttf(字體)文件用於私人字體集合

[英](VB.net) How do I use a .ttf (font) file for a private font Collection

好的,所以我正在嘗試為程序使用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編寫的。

我創建了一個使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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM