0

Okay, so I am trying to use a privatefontcollection for my program to add a little bit of uniqueness to it. The font that I am using is not installed on computers by default. The font's name is youmurdererbb_reg. I have the font file in the resources folder and the file is in a .ttf format. Here is what i have so far:

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

Now where i declared the Private Font Collection, I have tried these different things to get it to work:

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)

But whichever one I choose, it just shows the entire form with a large red "X" (Like a pictureboxes' "ErrorImage") (I have a picture set to the forms background as additional information).

Another problem is that if i don't try to convert it:

Dim YouMurderer As String = My.Resources.youmurdererbb_reg

Then it comes up with the error of:

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

I need help with this in .NET (Framework 4)! The entire program is being written in VB.net, not C#, or C++, or JAVA.

Richard Paulicelli
  • 129
  • 3
  • 6
  • 23

1 Answers1

0

I've created a library that makes this easy called BizArk. You can install it using NuGet, or, if you just want to use the source as a reference, you can get to the code here (Current/BizArkCore/Util/FontUtil.cs). Note that the code is in C#, but's there's not much code, so hopefully you should be able to follow along.

If you use the FontUtil class as it is, you can use it to create any font, even built in fonts. Here's how to use it...

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
Brian
  • 37,399
  • 24
  • 94
  • 109
  • Hrmmm, I installed the nuget, and the bizark package, however, it's not recognizing FontUtil as any declared function. Is there something that I am supposed to import first? – Richard Paulicelli Feb 10 '12 at 19:28
  • You will want to import the namespace. It should be @Import BizArk.Core.Util (from memory :) – Brian Feb 10 '12 at 19:34
  • BTW, I just guessed on the name of the font in my example above, you will probably need to figure out what name you need to use which isn't always a simple task. – Brian Feb 10 '12 at 19:35
  • Okay well I used the namespace for the import and it wasn't working at first, then out of nowhere it just started to work xD. Then another error showed up about the packages.config. and it showing this error for the initial tag "The 'packages' element is not declared." This should not be the case as it should automatically generated correct? Anyway I truly appreciate the help you've given me so far. And will continue to appreciate any further assistance you give me :). – Richard Paulicelli Feb 10 '12 at 19:43
  • Sorry, I don't know why the NuGet reference seems to be corrupt. One thing you can do is just copy the BizArk dll to a lib folder, remove the package reference from your project, then reference the dll directly (instead of using NuGet). You won't get the benefit of being notified of updates, but that's probably OK. – Brian Feb 10 '12 at 20:11
  • Okay, I fixed that issue (i just needed to delete it from my XNA Content package) and that's fine. Now It's saying that it cannot be found, even though i gave the exact name. and to be sure i also tried to include the extension. `code`Dim Fnt As Font = FontUtil.Create("youmurdererbb_reg.ttf", 80, FontStyle.Regular)`code` and I've tried using the `code`My.resources.youmurdererbb_reg`code` and it still didn't work, it gave me cannot convert byte to string error again. I know people have gotten this to work before, i just don't know how they did it! – Richard Paulicelli Feb 10 '12 at 23:35
  • The name of the font isn't the name of the file or the resource in the project. It's embedded in the font itself. You might need to look at the list of font families (FontUtil.GetAllFamilies), find the family you registered, and get the name from that. I've had problems with this before too. – Brian Feb 11 '12 at 00:04