locked
Using True Type Font Error: "Parameter Is Not Valid." RRS feed

  • Question

  • I'm developing a Windows desktop Form application using VS2019.  I am using a Google True Type Font Family, "Federo-Regular.ttf".  I want to use it for some controls on my Form1.  The custom font is loaded via a method when Form1 loads:

    Private Sub SetForm1CustomFont()
            Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
            Dim filePath As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Federo-Regular.ttf")
            privateFonts.AddFontFile(filePath)
            Dim font24 As New System.Drawing.Font(privateFonts.Families(0), 24)
            Dim font12 As New System.Drawing.Font(privateFonts.Families(0), 12)
    
            Me.LblLogo.Font = font24
            Me.LblProjects.Font = font24
            Me.BtnNew.Font = font12
            Me.BtnOpen.Font = font12
            Me.BtnCopy.Font = font12
            Me.BtnRename.Font = font12
            Me.BtnDelete.Font = font12
            Me.BtnExitApp.Font = font12
    End Sub

    However, when the form loads, I get an error "Parameter is not valid" at System.Drawing.FontFamily.GetName(Int32 language) at System.Drawing.FontFamily.get_Name()

    I placed the ttf file inside the Debug folder of the project.  Another thing, the Labels and Buttons affected are placed inside a SplitContainer panel.  I wonder if that's affecting this.

    Appreciate any help.


    Marilyn Gambone

    Tuesday, November 10, 2020 2:29 PM

All replies

  • Hi

    Well, I am sorry tht I can't offer much in the way of help as everyrhing you posted seems to work here as expected.

    I downloaded the font (can't say I like it 😊), put it same as your post and tested with 2 Labels and a Button in a SplitContainer

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim privateFonts As New Text.PrivateFontCollection()
        Dim filePath As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Federo-Regular.ttf")
        privateFonts.AddFontFile(filePath)
        Dim font24 As New Font(privateFonts.Families(0), 24)
        Dim font12 As New Font(privateFonts.Families(0), 12)
    
        Label2.Font = font12
        Label3.Font = font24
        Button1.Font = font24
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, November 10, 2020 3:31 PM
    Tuesday, November 10, 2020 3:30 PM
  • I also tested (Windows 10, VS 2019) => it works fine...
    Tuesday, November 10, 2020 3:59 PM
  • Hi,

    Yeah.  Something must be going on.  I have so many input forms that I decided to scrap it; not worth it.  I wanted something that would be built into the executable file, so people who don't have the Federo font in their system don't have to install them.  I need for users to just get the executable file from me and they're ready to go.  I've placed it in the Resources folder, but still doesn't work.

    Anyway...Thanks.


    Marilyn Gambone

    Tuesday, November 10, 2020 9:57 PM
  • Hi deskcheck1,

    Thanks for your feedback.

    >>I've placed it in the Resources folder, but still doesn't work.

    I downloaded the font and added it in the Resource folder.

    The following code works for me.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim privateFonts As New Text.PrivateFontCollection()
            Dim fontBytes As Byte() = My.Resources.Federo_Regular
            Dim fontData = Marshal.AllocCoTaskMem(fontBytes.Length)
            Marshal.Copy(fontBytes, 0, fontData, fontBytes.Length)
            privateFonts.AddMemoryFont(fontData, fontBytes.Length)
            Dim font24 As New Font(privateFonts.Families(0), 24)
            Label1.Font = font24
        End Sub

    Result of my test.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hi

    I tried that  exact same example (from StackOverflow I think) and I found the style to be different to the style shown for the font on Google.  Would you try your code using a string in the Label that uses some of the more distinctive characters please. (Such as ABEGJQU fff jjj ttt)

    When I tried it, the characters were completely different to the Google example.


    Regards Les, Livingston, Scotland

    Wednesday, November 11, 2020 9:53 AM
  • Use AddFontMemResourceEx

    =>

    Dim bytes() As Byte = My.Resources.Federo_Regular
    Dim p As IntPtr = Marshal.AllocCoTaskMem(bytes.Length)
    Marshal.Copy(bytes, 0, p, bytes.Length)
    Dim nNumFonts As Integer = 0
    AddFontMemResourceEx(p, bytes.Length, IntPtr.Zero, nNumFonts)
    Dim fonts As PrivateFontCollection = New PrivateFontCollection()
    fonts.AddMemoryFont(p, bytes.Length)
    Marshal.FreeCoTaskMem(p)
    Label1.Font = New Font(fonts.Families(0), 24)

    <DllImport("Gdi32.dll", SetLastError:=True)>
    Private Shared Function AddFontMemResourceEx(ByVal pFileView As IntPtr, ByVal cjSize As Integer, ByVal pvResrved As IntPtr, <Out> ByRef pNumFonts As Integer) As IntPtr
    End Function



    Wednesday, November 11, 2020 10:20 AM
  • Hi leshay,

    Thank you for reminding me of this problem. You are right, the style of font in my test is different to the style shown for the font on Google, so I have deleted my reply.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 12, 2020 1:57 AM
  • I tried this but it's giving me an error on the "AddFontMemResourceEx(p, bytes.Length, IntPtr.Zero, nNumFonts)" function:

    "The debugger is unable to evaluate this expression"


    Marilyn Gambone

    Thursday, November 12, 2020 1:23 PM
  • Hi,

    I tried this, too, but it does change/override the default "Microsoft Sans Serif" font.


    Marilyn Gambone

    Thursday, November 12, 2020 1:27 PM
  • Hi

    OK, I have played around with this issue a little, gained a few more gray hairs, but *maybe* have a workable solution.  This is the very first time I have used the Publish option so hopefully I haven't made any rookie mistakes in my understanding of the process.

    First of all I right clicked on the Resources item in the Solution Explores, chose to Add, Add a New Item, browsed to where the font file is and selected it then OK added it to the Resources.  Next, I right clicked on the font item in Solution Explorer, chose Properties and in the properties pane, change the 'Build Action' to Content, and the 'Copy to Output Directory' to Copy is newer.

    *

    Then I used this code to test things firstly from the Editor in both Debug mode and Release - both wotked OK and displayed the items with the subject Font.

    *

    Now the Publish part.  I chose to Publish to a DeskTop Folder location. I selected the CDROM option and finalised the process.  The Folder was created and populated with what I imagine is the common items for this process,  including Setup.exe   etc.  In the Application Files, there was a Resources.Federo-Regular.ttf.deploy item.

    *

    I tried the setup.exe and lo and very much behold, the application ran AND had the correct font displayed - I am claiming that as a victory!

    *

    Anyway, *maybe* this would work for OP.

    *

    Here is the code I used.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim MyFonts As Text.PrivateFontCollection = New Text.PrivateFontCollection
    	Dim CurrFont As Font
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		Dim bn As String = IO.Path.Combine(Application.StartupPath, "Resources", "Federo-Regular.ttf")
    
    		MyFonts.AddFontFile(bn)
    		CurrFont = New Font(MyFonts.Families(0), 120, FontStyle.Regular, GraphicsUnit.Document, 0)
    
    		Dim teststring As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    		With Label1
    			.Text = teststring
    			.Font = New Font(CurrFont.FontFamily, 34, FontStyle.Regular)
    		End With
    
    		With Label2
    			.Text = teststring.ToLower
    			.Font = New Font(CurrFont.FontFamily, 48, FontStyle.Regular)
    		End With
    
    		With Label3
    			.Text = "1234567890"
    			.Font = New Font(CurrFont.FontFamily, 64, FontStyle.Regular)
    		End With
    
    		With Label4
    			.Text = "1234567890"
    			.Font = New Font(CurrFont.FontFamily, 32, FontStyle.Bold)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    Thursday, November 12, 2020 7:27 PM
  • Hi,

    I tried this, too, but it does change/override the default "Microsoft Sans Serif" font.

    I don't see what you mean

    "Microsoft Sans Serif" is not changed =>

    Thursday, November 12, 2020 7:59 PM
  • Hi deskcheck1,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 19, 2020 5:49 AM
  • No.  Decided not to use TTF.  Too much trouble.  It changes but when I click other buttons, the buttons is marked as X with no button text.  Too confusing.  Thanks.

    Marilyn Gambone

    Thursday, December 10, 2020 4:52 PM