zoukankan      html  css  js  c++  java
  • C# JackLib系列之字体使用

    字体的使用一般我们都是使用系统字体,这样比较方便,直接 Font font=new Font("微软雅黑",16f,FontStyle.Bold);

    但是当我们用到一个系统没有的字体库时,这个方法就不好用了,因此我们可以采用动态加载字体文件的方式或者直接把字体打包到我们的程序集里当作资源来使用;

    下面我们来看一下怎么用:

    我封装了一个类,大家可以直接使用,如果有不好的地方,欢迎大家指正。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing.Text;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    
    namespace jackLib.fonthelper {
        /// <summary>
        /// 字体库帮助类
        /// </summary>
        public class FontHelper {
            /// <summary>
            /// 通过字体文件获取字体
            /// </summary>
            /// <param name="fontPath"></param>
            /// <param name="fontSize"></param>
            /// <returns></returns>
            public static Font GetFontFromFile(string fontPath, float fontSize,FontStyle fontStyle) {
                try {
                    //校验
                    if (!File.Exists(fontPath) || fontSize <= 0) {
                        return null;
                    }
                    //获取字体对象
                    PrivateFontCollection fontCollection = new PrivateFontCollection();
                    fontCollection.AddFontFile(fontPath);
                    var font = new Font(fontCollection.Families[0], fontSize, fontStyle);
                    return font;
                }
                catch (Exception ex) {
                    throw ex;
                }
            }
    
            /// <summary>
            /// 通过资源流获取字体
            /// </summary>
            /// <param name="fontName"></param>
            /// <param name="fontSize"></param>
            /// <returns></returns>
            public static Font GetFontFromStream(string fontName, float fontSize, FontStyle fontStyle) {
                try {
                    //获取程序集
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    //获取字体文件流
                    Stream stream = assembly.GetManifestResourceStream(fontName);
    
                    //读取字体到字节数组
                    byte[] fontData = new byte[stream.Length];
                    stream.Read(fontData, 0, (int)stream.Length);
                    stream.Close();
    
                    //获取字体对象
                    PrivateFontCollection pfc = new PrivateFontCollection();
                    unsafe {
    
                        fixed (byte* pFontData = fontData) {
                            pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length);
                        }
                    }
    
                    return new Font(pfc.Families[0], fontSize, fontStyle);
                }
                catch (Exception ex) {
                    throw ex;
                }
    
            }
        }
    }
    

     使用方法如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace jackLib.fonthelper {
        /// <summary>
        /// 字体使用测试
        /// </summary>
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
    
            protected override void OnLoad(EventArgs e) {
                try {
                    //第1种用法
                    var font1 = FontHelper.GetFontFromStream("jackLib.fonthelper.Font.SourceCodePro-It.ttf", 20, FontStyle.Italic);
                    this.textBox1.Font = font1 ?? this.textBox1.Font;
    
                    //第2种用法
                    var font2 = FontHelper.GetFontFromFile(@"FontSourceCodePro-It.ttf", 20, FontStyle.Regular);
                    this.textBox1.Font = font2 ?? this.textBox1.Font;
    
                }
                catch (Exception ex) {
                    throw ex;
                }
                finally {
                    base.OnLoad(e);
                }
            }
        }
    }
    
  • 相关阅读:
    665. Non-decreasing Array
    35. Search Insert Position
    CompositePattern(组合模式)-----Java/.Net
    BridgePattern(桥接模式)-----Java/.Net
    AdapterPattern(适配器模式)-----Java/.Net
    设计模式系列:原型模式(Prototype Pattern)
    设计模式系列:抽象工厂模式(Abstract Factory Pattern)
    设计模式系列:工厂方法模式(Factory Method Pattern)
    设计模式系列:简单工厂模式(Simple Factory Pattern)
    设计模式系列:单例模式(Singleton Pattern)
  • 原文地址:https://www.cnblogs.com/shaozhuyong/p/5768922.html
Copyright © 2011-2022 走看看