|
-
Jan 2nd, 2019, 07:26 PM
#1
Thread Starter
Hyperactive Member
Using Custom Fonts
In my forms load event I placed the following code to load and use a custom font I purchased (all licensed and good to go for projects):
IO.File.WriteAllBytes("test.ttf", My.Resources.RaintreeBold)
Dim pfc As New PrivateFontCollection()
pfc.AddFontFile("test.ttf")
MyLabel.Font = New Font(pfc.Families(0), 14)
When I run the program I get a "Parameter is not Valid" general error, but not as the code above is executed. It happens on an Update() or .ShowDialog() call right before the window is displayed.
What is truly odd is that sometimes the code works; I see my label with the correct font. Then I run the program again and it doesn't having changed nothing. Then I run it again and the error occurs again.
From what I have researched the "Parameter is not Valid" error is happening during the GDI drawing of the form. Is the font somehow lost after it is initially loaded in the Load event? What am I doing wrong here for what should be a basic task?
Thanks in advance.
Intermediate Level Programmer Extraordinaire 
-
Jan 2nd, 2019, 08:38 PM
#2
Re: Using Custom Fonts
The fact that the result is intermittent success, my first thought is that there is some sort of timing issue where the newly written font file is sometimes being accessed before the file has been completely created.
To test that theory out, if you put some sort of a delay in between the font file being created and it being accessed you might see more consistent results.
-
Jan 3rd, 2019, 08:00 AM
#3
Re: Using Custom Fonts
I'd probably also put in a check for the font first, rather than blindly copying it every time. Once it's there, there really shouldn't be a need to copy it again. It's possible (remotely) that it's that additional copying that could be the issue.
-tg
-
Jan 3rd, 2019, 08:08 AM
#4
Re: Using Custom Fonts
I'm wondering why a resource is being extracted to a file in the first place. Are you trying to stop people making copies of the font file? If not, why not distribute the font as a file in the first place and forget the resource?
-
Jan 3rd, 2019, 01:06 PM
#5
Thread Starter
Hyperactive Member
Re: Using Custom Fonts
 Originally Posted by jmcilhinney
I'm wondering why a resource is being extracted to a file in the first place. Are you trying to stop people making copies of the font file? If not, why not distribute the font as a file in the first place and forget the resource?
I get the same error whether I extract to a file or use AddFontFile(). I tried both hoping one would work, but neither did.
Intermediate Level Programmer Extraordinaire 
-
Jan 3rd, 2019, 01:07 PM
#6
Thread Starter
Hyperactive Member
Re: Using Custom Fonts
 Originally Posted by techgnome
I'd probably also put in a check for the font first, rather than blindly copying it every time. Once it's there, there really shouldn't be a need to copy it again. It's possible (remotely) that it's that additional copying that could be the issue.
-tg
I suspect this might be the cause. How do I create a check for the font?
Intermediate Level Programmer Extraordinaire 
-
Mar 3rd, 2019, 10:43 AM
#7
Thread Starter
Hyperactive Member
Re: Using Custom Fonts
So this is a thread from months ago that I wanted to update in case anyone else encounters the same problem.
Upon returning to this problem I found a simple fix. I changed my PrivateFontCollection object (pfc) to a private variable in the form. No more error!
Intermediate Level Programmer Extraordinaire 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|