【问题标题】:How can I convert a System.Drawing.font to a System.Windows.Media.Fonts or TypeFace?如何将 System.Drawing.font 转换为 System.Windows.Media.Fonts 或 TypeFace?
【发布时间】:2010-11-20 19:57:25
【问题描述】:

如何将System.Drawing.Font 转换为System.Windows.Media.FontsTypeFace

或者如何从System.Drawing.Font 的实例生成System.Windows.Media.FontsTypeFace 的实例?

【问题讨论】:

    标签: c# fonts


    【解决方案1】:

    您无法实例化 Media.Fonts ,但我认为您可以获得 Media.FontFamily 这就是我实现它的方式。

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

    【讨论】:

    • 如果系统上还没有安装字体怎么办?也许我们想在安装前显示字体预览。
    • 如果我的字体来自系统上未安装的嵌入式资源怎么办??
    【解决方案2】:

    我正在使用以下代码

    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2013-09-21
      • 1970-01-01
      • 2014-11-05
      • 2010-12-28
      • 2010-10-28
      • 1970-01-01
      相关资源
      最近更新 更多