Can someone example how to embed font into the project?
Printable View
Can someone example how to embed font into the project?
Hey,
Have a look at this article:
http://www.bobpowell.net/embedfonts.htm
Hopefully it will have the information that you are looking for.
Gary
Would I put that code in my main.cs file or create a new .cs file?
Hey,
That really depends on what it is that you want to do with the embedded font?
I have to be honest and say that I have never played with doing this, I simply found the article after a quick google.
What exactly are you trying to achieve?
Gary
When you install my program on another computer and the font is missing it display an error message saying that the font is missing, continue or quit. When you continue it doesn't show up right.
Hello,
In which case, you would need to do what the first part of the article suggests, and load the font into a PrivateFontCollection. From there, you can take control of setting the font in the textboxes, labels etc within your application.
The reason that you are getting the error message is that you have set the font already based on a font that you have on your machine. Then, when you try to run it on a machine that doesn't have that font, you get this message, and when you hit continue, it will find the next closest font.
What you want to do instead is at design time set the font to something you know will be there, i.e. Arial, then at run time, set the Font to be the Font from the PrivateFontCollection.
The other way to do it would be to install the required Font as part of your project's installation.
Gary
Hey,
What I mean is, it sounds like you are deploying your application to a system that doesn't have the same font as you have installed on your machine. Is this the case?
If so, then you could create a setup project for your application, and as part of the installation, install the font that you want used with your application.
You can find an example of doing this here:
http://pinvoke.net/default.aspx/gdi3...tResource.html
Gary
When I drop this into my main.cs file the Dim gets an error (namespace does not directly contain members such as fields or methods). What could be the problem?
Code:Dim pfc As New PrivateFontCollection()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fontStream As Stream = Me.GetType().Assembly.GetManifestResourceStream("embedfont.vb.segoeui.ttf")
Dim data As System.IntPtr = Marshal.AllocCoTaskMem(fontStream.Length)
Dim fontdata() As Byte
ReDim fontdata(fontStream.Length)
fontStream.Read(fontdata, 0, fontStream.Length)
Marshal.Copy(fontdata, 0, data, fontStream.Length)
pfc.AddMemoryFont(data, fontStream.Length)
fontStream.Close()
Marshal.FreeCoTaskMem(data)
End Sub
Hey,
The article that I linked you to was written in VB.Net, since you are coding in C# you are going to need to convert it.
Gary
How would I convert it?
There are a number of ways of doing this...
However, before telling you them, I should point out that the important thing here is understanding what the code is trying to do, and why it is doing it. Once you have done that, it is just a matter of syntax. At the end of the day, both C# and VB.Net compile down to the same Common Language Runtime.
One way of converting the code would be to use one of the online converters, for instance:
http://www.developerfusion.com/tools.../vb-to-csharp/
However, the method that I would recommend is to look at the code, figure out what classes and methods are being used, and then look at the documentation for them. For instance:
http://msdn.microsoft.com/en-us/libr...on(VS.71).aspx
Gary
I'm still unable to get this to work...
Hey,
You are going to have to provide some more detail than that?!?
What have you tried? Why didn't it work? Were there any exceptions?
Without more information, nobody is going to be able to help you.
Gary
have tried this code, but unsafe, and this line AddFontMemResourceEx((IntPtr)pFontData, (uint)Resources.MyFont.Length, IntPtr.Zero, ref dummy); has errors..
Code:
class AnyClass
{
// This is used to correct a bug in GDI+.
// Private fonts return garbage in GetKerningPairs if they are not installed on the system.
// Also, they don't print correctly.
[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
private PrivateFontCollection pfc = new PrivateFontCollection();
public AnyClass()
{
unsafe
{
fixed (byte* pFontData = Resources.MyFont)
{
pfc.AddMemoryFont((IntPtr)pFontData, Resources.MyFont.Length);
AddFontMemResourceEx((IntPtr)pFontData, (uint)Resources.MyFont.Length, IntPtr.Zero, ref dummy);
}
}
}
}
I have also tried this code, but it doesn't work.
Code:1) Create new project (myFont).2) Create label(lblUsemyFontName). 3) Add your font file (myFontName.ttf) to Resourcesusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Text;namespace myFont{ public partial class Form1 : Form { [System.Runtime.InteropServices.DllImport("gdi32.dll")] private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts); private PrivateFontCollection MYpfc = new PrivateFontCollection(); public Form1() { InitializeComponent(); try { unsafe { fixed (byte* pFontData = Properties.Resources.myFontName) { uint dummy = 0; MYpfc.AddMemoryFont((IntPtr)pFontData, Properties.Resources.myFontName.Length); AddFontMemResourceEx((IntPtr)pFontData, (uint)Properties.Resources.myFontName.Length, IntPtr.Zero, ref dummy); } } } catch { MessageBox.Show("For correct display the application should run on Windows 2000, XP or Vista (only in full trust zone, local disk)."); } } private void Form1_Load(object sender, EventArgs e) { lblUsemyFontName.Font = new Font(MYpfc.Families[0], 36); //Font size is 36 // lblUsemyFontName1.Font = new Font(MYpfc.Families[0], 22); //Font size is 22 // lblUsemyFontName2.Font = new Font(MYpfc.Families[0], 16); //Font size is 16 } }}
Does anyone have anything on why this isn't working?
Or do I need to make different version of my application for different version of windows that uses a font that would work there the best and not come up with an error sorry missing font continue or quit; and then you continue and it doesn't work?
I have tried this code, but getting error on DestinationFile
Code:class Program
{
[DllImport("gdi32", EntryPoint = "AddFontResource")]
public static extern int AddFontResourceA(string lpFileName);
static void Main()
{
string[] arr = Environment.GetCommandLineArgs();
String FontPath = arr[1];
AddFontResourceA(DestinationFile);
}
}
Hey,
Where is DestinationFile declared? I don't see it in the code that you have posted?!?!
Gary
This is just code I found on the internet, but I'm using a self installer program to install fonts if they don't exists.
Hey,
If you are going to use code off the internet, you are going to need to spend some time understanding what it is it is doing, before implementing it.
In this case, a string needs to be past into the AddFontResourceA, in the code you have posted, this string is held in a variable called DestinationFile. You will either need to create this variable and assign it a value, or replace it with a valid string.
Gary