Answered by:
PrivateFontCollection does not work

Question
-
User539648229 posted
Hi
I'm trying to use fonts on the server that is not installed but it seems that PrivateFontCollection does not work. Here is my code:
protected void Page_Load(object sender, EventArgs e) { Image image = new Bitmap(600, 400); Graphics g = Graphics.FromImage(image); PrivateFontCollection fonts = new PrivateFontCollection(); fonts.AddFontFile(HttpContext.Current.Request.PhysicalApplicationPath + "\\Fonts\\NellaSueDEMO.ttf"); Font font = new Font(fonts.Families[0].Name, 20); g.DrawString("Sample", font, new SolidBrush(Color.Black), 300, 200); Response.ContentType = "image/png"; image.Save(Response.OutputStream, ImageFormat.Png); g.Dispose(); image.Dispose(); Response.Flush(); }
Please Help
ThanksSunday, April 6, 2014 8:37 AM
Answers
-
User753101303 posted
Hi,
I gave a try using a Windows app and :
PrivateFontCollection pfc = new PrivateFontCollection(); var f=System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); f=f+"\\la_unica\\launica.ttf"; pfc.AddFontFile(f); foreach (var family in pfc.Families) { var myFont = new Font(family, 18); label1.Font = myFont; label1.Text="Hello World."; break; }
worked for me. It didn't work when using the family name rather than a family object. What was the error you had when trying this approach ? Have you tried with other font files ? I tested with La Unica from http://www.1001freefonts.com/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 9, 2014 7:04 AM
All replies
-
User753101303 posted
Hi,
Note sure but what if you try http://msdn.microsoft.com/en-us/library/8882dhsk(v=vs.110).aspx that is to initialize the font with a family object rather than with a family name (in the second case I don't see how it could guess it has to use the PrivateFontCollection, in the second case, the family object does come from this collection) ? At least it would be my first try.
Edit: so I would try Font font = new Font(fonts.Families[0], 20); rather than Font font = new Font(fonts.Families[0].Name, 20);
Sunday, April 6, 2014 2:21 PM -
User465171450 posted
Are you receiving an error? If so, can you post it here?
If Fonts is a directory off the root of your application, try
fonts.AddFontFile(HttpContext.Current.Server.MapPath("~/Fonts/NellaSueDEMO.ttf"));
This will return the physical path from the virtual file's path based on the root of this current application.
Tuesday, April 8, 2014 12:44 AM -
User539648229 posted
Hi
The documention says that name must be used and if I use Family, Visual Studio crashes. there is no error and result is displayed with default font and not with NellaSue(script font style). Originally I used a persian font and it did not work and I tested it with a english font(NellaSue) from a website but the result was the same. I searced the PrivateFontCollection in the forum and it seems other poeple used it successfully. I think there is a point using it or there is a problem with my OS(Win 8).
Regards
Tuesday, April 8, 2014 3:57 AM -
User753101303 posted
Try perhaps http://msdn.microsoft.com/en-us/library/y505zzfw(v=vs.110).aspx with your font file. Use the same overload and it seems that they also included the style as part of the name.
Does the same work in Windows 7 ?
Tuesday, April 8, 2014 4:30 AM -
User539648229 posted
Hi
I used the same overload but nothing changed. They included the style as part of the name for drawing it on the form. Both my laptop and desktop have win 8 installed on them. I would be thankful if you test it on you computer.
Regards
Wednesday, April 9, 2014 6:17 AM -
User753101303 posted
Hi,
I gave a try using a Windows app and :
PrivateFontCollection pfc = new PrivateFontCollection(); var f=System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); f=f+"\\la_unica\\launica.ttf"; pfc.AddFontFile(f); foreach (var family in pfc.Families) { var myFont = new Font(family, 18); label1.Font = myFont; label1.Text="Hello World."; break; }
worked for me. It didn't work when using the family name rather than a family object. What was the error you had when trying this approach ? Have you tried with other font files ? I tested with La Unica from http://www.1001freefonts.com/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 9, 2014 7:04 AM -
User539648229 posted
Hi
I checked again and this time it was Ok with Families. The day I started this thread, IIS crashed serveral times and I used name. It seems that working with fonts is unstable because in my applicatin sometimes font size changes. Thanks anyway.
Thursday, April 10, 2014 6:12 AM