|
-
Jul 4th, 2009, 11:46 AM
#1
Thread Starter
Junior Member
Embedding Fonts
Can someone example how to embed font into the project?
-
Jul 5th, 2009, 10:07 AM
#2
Re: Embedding Fonts
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
-
Jul 5th, 2009, 10:14 AM
#3
Thread Starter
Junior Member
Re: Embedding Fonts
Would I put that code in my main.cs file or create a new .cs file?
-
Jul 5th, 2009, 10:22 AM
#4
Re: Embedding Fonts
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
-
Jul 5th, 2009, 10:27 AM
#5
Thread Starter
Junior Member
Re: Embedding Fonts
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.
-
Jul 5th, 2009, 10:37 AM
#6
Re: Embedding Fonts
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
-
Jul 5th, 2009, 10:44 AM
#7
Thread Starter
Junior Member
Re: Embedding Fonts
 Originally Posted by gep13
Hello,
The other way to do it would be to install the required Font as part of your project's installation.
Gary
Please Explain how I would require the font at installation?
-
Jul 5th, 2009, 10:53 AM
#8
Re: Embedding Fonts
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
-
Jul 5th, 2009, 11:17 AM
#9
Thread Starter
Junior Member
Re: Embedding Fonts
 Originally Posted by gep13
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
-
Jul 5th, 2009, 11:29 AM
#10
Re: Embedding Fonts
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
-
Jul 5th, 2009, 11:39 AM
#11
Thread Starter
Junior Member
-
Jul 5th, 2009, 11:46 AM
#12
Re: Embedding Fonts
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
-
Jul 7th, 2009, 06:58 AM
#13
Thread Starter
Junior Member
Re: Embedding Fonts
I'm still unable to get this to work...
-
Jul 7th, 2009, 07:01 AM
#14
Re: Embedding Fonts
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
-
Jul 12th, 2009, 11:29 AM
#15
Thread Starter
Junior Member
Re: Embedding Fonts
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?
-
Jul 12th, 2009, 12:22 PM
#16
Thread Starter
Junior Member
Re: Embedding Fonts
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);
}
}
-
Jul 18th, 2009, 04:02 AM
#17
Re: Embedding Fonts
Hey,
Where is DestinationFile declared? I don't see it in the code that you have posted?!?!
Gary
-
Jul 18th, 2009, 07:16 AM
#18
Thread Starter
Junior Member
Re: Embedding Fonts
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.
-
Jul 18th, 2009, 08:11 AM
#19
Re: Embedding Fonts
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
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
|