login  Naam:   Wachtwoord: 
Registreer je!
 Forum

[c#] Embedded font gebruiken

Offline mike643 - 24/11/2008 11:44
Avatar van mike643Nieuw lid Ik wil graag een embedded font gebruiken in mijn applicatie. Nu heb ik het voorbeeld gebruikt van www.bobpowell.net/embedfonts.htm.
Helaas krijg ik in mij printdocument1 gewoon een standaard lettertype te zien ipv. mijn Alphd___.ttf.
Iemand een idee waar dit aan zou kunnen liggen?

De code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Drawing.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace WindowsApplication5
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. PrivateFontCollection pfc = new PrivateFontCollection();
  21.  
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("WindowsApplication5.Alphd___.ttf");
  25.  
  26.  
  27.  
  28. byte[] fontdata = new byte[fontStream.Length];
  29.  
  30. fontStream.Read(fontdata, 0, (int)fontStream.Length);
  31.  
  32. fontStream.Close();
  33.  
  34. unsafe
  35. {
  36.  
  37. fixed (byte* pFontData = fontdata)
  38. {
  39.  
  40. pfc.AddMemoryFont((System.IntPtr)pFontData, fontdata.Length);
  41.  
  42. }
  43.  
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. }
  52.  
  53. private void button1_Click(object sender, EventArgs e)
  54. {
  55. printPreviewDialog1 = new PrintPreviewDialog();
  56. printPreviewDialog1.Document = printDocument1;
  57. printPreviewDialog1.Show();
  58. printPreviewDialog1.Activate();
  59. printPreviewDialog1.BringToFront();
  60. }
  61.  
  62. private void printPreviewDialog1_Load(object sender, EventArgs e)
  63. {
  64.  
  65. }
  66.  
  67. private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  68. {
  69.  
  70. bool bold = false;
  71.  
  72. bool regular = false;
  73.  
  74. bool italic = false;
  75.  
  76.  
  77.  
  78. e.Graphics.PageUnit = GraphicsUnit.Point;
  79.  
  80. SolidBrush b = new SolidBrush(Color.Black);
  81.  
  82.  
  83.  
  84. float y = 5;
  85.  
  86.  
  87.  
  88. System.Drawing.Font fn;
  89.  
  90.  
  91.  
  92. foreach (FontFamily ff in pfc.Families)
  93. {
  94.  
  95. if (ff.IsStyleAvailable(FontStyle.Regular))
  96. {
  97.  
  98. regular = true;
  99. fn = new Font(ff, 18, FontStyle.Regular);
  100.  
  101. e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
  102.  
  103. fn.Dispose();
  104.  
  105. y += 20;
  106.  
  107. }
  108.  
  109. if (ff.IsStyleAvailable(FontStyle.Bold))
  110. {
  111.  
  112. bold = true;
  113.  
  114. fn = new Font(ff, 18, FontStyle.Bold);
  115.  
  116. e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
  117.  
  118. fn.Dispose();
  119.  
  120. y += 20;
  121.  
  122. }
  123.  
  124. if (ff.IsStyleAvailable(FontStyle.Italic))
  125. {
  126.  
  127. italic = true;
  128.  
  129. fn = new Font(ff, 18, FontStyle.Italic);
  130.  
  131. e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
  132.  
  133. fn.Dispose();
  134.  
  135. y += 20;
  136.  
  137. }
  138.  
  139. if (bold && italic)
  140. {
  141.  
  142. fn = new Font(ff, 18, FontStyle.Bold | FontStyle.Italic);
  143.  
  144. e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
  145.  
  146. fn.Dispose();
  147.  
  148. y += 20;
  149.  
  150. }
  151.  
  152. fn = new Font(ff, 18, FontStyle.Underline);
  153.  
  154. e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
  155.  
  156. fn.Dispose();
  157.  
  158. y += 20;
  159.  
  160. fn = new Font(ff, 18, FontStyle.Strikeout);
  161.  
  162. e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
  163.  
  164. fn.Dispose();
  165.  
  166. }
  167.  
  168.  
  169.  
  170. b.Dispose();
  171.  
  172.  
  173. }
  174. }
  175. }

0 antwoorden

Gesponsorde links
Er zijn nog geen reacties op dit bericht.
Dit onderwerp is gesloten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.163s