字体的使用一般我们都是使用系统字体,这样比较方便,直接 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(@"Font\SourceCodePro-It.ttf", 20, FontStyle.Regular);
this.textBox1.Font = font2 ?? this.textBox1.Font; }
catch (Exception ex) {
throw ex;
}
finally {
base.OnLoad(e);
}
}
}
}

C# JackLib系列之字体使用的更多相关文章

  1. DirectX 基础学习系列6 字体

    DIRECTX9自带ID3DXFONT类 内部调用GDI的接口,效率一般,但能够处理一些复杂的字体 HRESULT D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 ...

  2. C# JackLib系列之GdiHelper圆角矩形的快速生成

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. CSS3学习系列之字体

    给文字添加阴影 在CSS3中,可以使用text-shadow属性给页面上的文字添加阴影效果.text-shadow属性是在css2中定义的,在css2.1中删除,在css3的text模块中有恢复了,用 ...

  4. SDL系列之 - 字体显示测试

    例9.7:设计一个程序,初始化视频子系统,设置显示模式为640*480,表面的色深为16位,使用SDL_ttf库在屏幕上显示“Linux下TrueType字体显示示例”,字体大小为38,颜色为红色.设 ...

  5. C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

    获取地球上两经纬度坐标点间的距离,利用[大圆距离公式]   A diagram illustrating great-circle distance (drawn in red) between tw ...

  6. C# JackLib系列之Form窗体的ShowWithoutActivation属性及其作用

    代码改变世界! 如果要显示顶级窗口,但又不希望由于将输入焦点从当前窗口移开而中断用户的工作,请使用此属性.它可以是一个信息性弹出窗口或浮动窗口,如“画图”应用程序中的“工具”调色板. 由于此属性为只读 ...

  7. C# JackLib系列之自定义鼠标风格的实现

    在我们开发的过程中,有时需要我们来自定义鼠标的形状和大小,刚巧前一阵子正好用到了这个技术,找了好多资料,基本上都是黑白色的鼠标风格实现,而我要的则是自定义大小和彩色风格的光标样式.百度上的资源又太少, ...

  8. CSS字体

    字体系列 [1]5种通用字体系列:拥有相似外观的字体系列 serif字体:字体成比例,且有上下短线,包括Times\Georgia\New century Schoolbook sans-serif字 ...

  9. css整理-02 颜色和字体

    颜色 命名颜色 RGB指定颜色 数值: 0-255 百分比 三元组:红绿蓝 16进制RGB web安全颜色 在256色计算机系统上总能避免抖动的颜色 表示为rgb值20%和51的倍数 web安全色的简 ...

随机推荐

  1. js客户端UI框架

    Best jQuery UI http://b-jui.com/ jQuery EasyUI http://www.jeasyui.com/ bootstrap学习网: http://www.runo ...

  2. PCB 锣板和半孔工艺的差别

    PCB 锣板和半孔工艺的差别 PCB 在做模块时会用到半孔工艺,但是由于半孔是特殊工艺. 需要加费用,打板时费还不低. 下面这个图是锣板和半孔工艺的差别. https://www.amobbs.com ...

  3. java中十进制转换为任意进制

    次笔试时候遇到的关于十进制转换成十三进制的编程题. 先说说简单的思路吧: 1.十进制数 num 转换为 n进制 num%n结果肯定为n进制数的最后一位 结果存入一个数组中 2.进入一个循环num!=0 ...

  4. delphi 线程的使用

    unit untWorkThread; interface uses Windows,Classes,SysUtils; type TWorkItem=class end; TProcessWork= ...

  5. 获取设备 ID 和名称

    获取设备 ID 和名称 .NET Framework 3.5   其他版本   更新:2007 年 11 月 要获取设备的名称,请使用 Dns.GetHostName 属性.通常情况下,默认名称为“P ...

  6. codeforces 985E Pencils and Boxes(dp+思维)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. python开发进程:共享数据&进程池

    一,共享数据 展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合 通过消息队列交换数据.这样极大地减少了对使用锁定和其他同步手段的需求, 还可以扩展 ...

  8. Bash读取/etc/passwd的特殊技巧

    在twitter上看到的,记录一下: 可以bypass基于正则的规则,这里还可以引申出其他的bypass方法,

  9. php代码执行漏洞

    php代码执行的两个函数eval(),assert() <?php $i = $_GET['x']; eval($i); ?> eval()函数将以php类型执行传入的参数x的值 给x传入 ...

  10. 航图转换相关sql

    --旧库列举所有类型航图 select distinct  c.identifier airport_icao,d.sc_code chart_type,s.namefrom special_char ...