timesking
timesking

Reputation: 275

How can I convert a System.Drawing.font to a System.Windows.Media.Fonts or TypeFace?

How can I convert a System.Drawing.Font to a System.Windows.Media.Fonts or TypeFace?

Or how can I generate an instance of System.Windows.Media.Fonts or TypeFace from an instance of System.Drawing.Font?

Upvotes: 12

Views: 22296

Answers (2)

timesking
timesking

Reputation: 275

I'm using below 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;

}

Upvotes: 5

Stan R.
Stan R.

Reputation: 16065

you cant instantiate Media.Fonts , but I think you can get a Media.FontFamily this is how I achieved it.

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();

Upvotes: 11

Related Questions