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(@"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系列之字体使用的更多相关文章
- DirectX 基础学习系列6 字体
DIRECTX9自带ID3DXFONT类 内部调用GDI的接口,效率一般,但能够处理一些复杂的字体 HRESULT D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 ...
- C# JackLib系列之GdiHelper圆角矩形的快速生成
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- CSS3学习系列之字体
给文字添加阴影 在CSS3中,可以使用text-shadow属性给页面上的文字添加阴影效果.text-shadow属性是在css2中定义的,在css2.1中删除,在css3的text模块中有恢复了,用 ...
- SDL系列之 - 字体显示测试
例9.7:设计一个程序,初始化视频子系统,设置显示模式为640*480,表面的色深为16位,使用SDL_ttf库在屏幕上显示“Linux下TrueType字体显示示例”,字体大小为38,颜色为红色.设 ...
- C# JackLib系列之如何获取地球上两经纬度坐标点间的距离
获取地球上两经纬度坐标点间的距离,利用[大圆距离公式] A diagram illustrating great-circle distance (drawn in red) between tw ...
- C# JackLib系列之Form窗体的ShowWithoutActivation属性及其作用
代码改变世界! 如果要显示顶级窗口,但又不希望由于将输入焦点从当前窗口移开而中断用户的工作,请使用此属性.它可以是一个信息性弹出窗口或浮动窗口,如“画图”应用程序中的“工具”调色板. 由于此属性为只读 ...
- C# JackLib系列之自定义鼠标风格的实现
在我们开发的过程中,有时需要我们来自定义鼠标的形状和大小,刚巧前一阵子正好用到了这个技术,找了好多资料,基本上都是黑白色的鼠标风格实现,而我要的则是自定义大小和彩色风格的光标样式.百度上的资源又太少, ...
- CSS字体
字体系列 [1]5种通用字体系列:拥有相似外观的字体系列 serif字体:字体成比例,且有上下短线,包括Times\Georgia\New century Schoolbook sans-serif字 ...
- css整理-02 颜色和字体
颜色 命名颜色 RGB指定颜色 数值: 0-255 百分比 三元组:红绿蓝 16进制RGB web安全颜色 在256色计算机系统上总能避免抖动的颜色 表示为rgb值20%和51的倍数 web安全色的简 ...
随机推荐
- C++的引用类型【转载】
c++比起c来除了多了类类型外还多出一种类型:引用.这个东西变量不象变量,指针不象指针,我以前对它不太懂,看程序时碰到引用都稀里糊涂蒙过去.最近把引用好好地揣摩了一番,小有收获,特公之于社区,让初学者 ...
- php查询mysql时,报超出内存错误(select count(distinct))时
学时服务器查询教练所带人数时,使用select count(distinct(u_STRNO))时报超出内存错误.后参考“mysqld-nt: Out of memory解决方法”http://jin ...
- MySQL写出高效SQL
mysql设计标准事务处理标准索引使用标准约束设计sql语句标准 怎么写出高效SQL清晰无误的了知业务需求满足业务需求,不做无用功知道表数据量和索引基本情况知道完成SQL需要扫描的数据量级SQL执行计 ...
- Java面试(二)
1 同步方法 VS 同步代码块: java中,每一个对象都有一把锁,线程用synchronized获取对象上的锁. 非静态同步方法:锁是类的对象的锁. 静态同步方法:锁的是类本身. 同步方法块:锁是可 ...
- [置顶]
TIM_GetCounter与TIM_GetCapture1的区别
/** * @brief Gets the TIMx Input Capture 1 value. * @param TIMx: where x can be 1 to 17 except 6 and ...
- ansible里的item和with_items
################################## 变量命名 变量名仅能由字母.数字和下划线组成且只能以字母开头. ################################# ...
- Linux性能监测:监测目的与工具介绍
性能监测是系统优化过程中重要的一环,如果没有监测.不清楚性能瓶颈在哪里,优化什么呢.怎么优化呢?所以找到性能瓶颈是性能监测的目的,也是系统优化的关键.本文对Linux性能监测的应用类型.底线和监测工具 ...
- vue-cli中的ESlint配置文件eslintrc.js详解
本文讲解vue-cli脚手架根目录文件.eslintrc.js eslint简介 eslint是用来管理和检测js代码风格的工具,可以和编辑器搭配使用,如vscode的eslint插件 当有不符合配置 ...
- CentOS7上elasticsearch5.5启动报错
ERROR: [2] bootstrap checks failed [1]: max file descriptors [4096] for elasticsearch process is too ...
- leetcode804
int uniqueMorseRepresentations(vector<string>& words) { map<char, string> st; st.ins ...