2009-08-19 2 views

Antwort

10

kippen Sie Media.Fonts, instanziiert aber ich denke, Sie Media.FontFamily bekommen das ist, wie ich es erreicht.

using System.Drawing; 
using Media = System.Windows.Media; 

Font font = new Font(new System.Drawing.FontFamily("Comic Sans MS"), 10); 
      //option 1 
      Media.FontFamily mfont = new Media.FontFamily(font.Name); 
      //option 2 does the same thing 
      Media.FontFamilyConverter conv = new Media.FontFamilyConverter(); 
      Media.FontFamily mfont1 = conv.ConvertFromString(font.Name) as Media.FontFamily; 
      //option 3 
      Media.FontFamily mfont2 = Media.Fonts.SystemFontFamilies.Where(x => x.Source == font.Name).FirstOrDefault(); 
+3

Was passiert, wenn die Schriftart nicht auf dem System noch nicht installiert ist? Vielleicht möchten wir vor der Installation eine Vorschau der Schriftart anzeigen. – SepehrM

+1

Was ist, wenn meine Schriftart von einer eingebetteten Ressource stammt, die nicht auf dem System installiert ist? – JacobD

5

Ich verwende unter Codes

private static Typeface NewTypeFaceFromFont(System.Drawing.Font f) 
{ 
    Typeface typeface = null; 

    FontFamily ff = new FontFamily(f.Name); 


    if (typeface == null) 
    { 
     typeface = new Typeface(ff, (f.Style == System.Drawing.FontStyle.Italic ? FontStyles.Italic : FontStyles.Normal), 
         (f.Style == System.Drawing.FontStyle.Bold ? FontWeights.Bold : FontWeights.Normal), 
            FontStretches.Normal); 
    } 
    if (typeface == null) 
    { 
     typeface = new Typeface(new FontFamily("Arial"), 
             FontStyles.Italic, 
             FontWeights.Normal, 
             FontStretches.Normal);    
    } 
    return typeface; 

} 
Verwandte Themen