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安全色的简 ...
随机推荐
- php webservice服务端和客户端的实现
1.创建类文件service.class.php,service类,添加若干方法. 2.用浏览器访问create_wsdl.php文件,生成service.wsdl文件. 3.修改wsdl文件,loc ...
- bzoj 3501 PA2008 Cliquers Strike Back——贝尔数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3501 用贝尔三角形 p^2 地预处理 p 以内的贝尔数.可以模(mod-1)(它是每个分解下 ...
- 【转】wireshark抓包工具详细说明及操作使用
wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必须了解网络协议,否则就看不懂wireshark了. 为了安全 ...
- 杂项:UN-标准通用置标语言
ylbtech-杂项:标准通用置标语言 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 ...
- nginx 配置隐藏index.php效果
location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; } } 完整如下 server { li ...
- php根据年月获取当月天数。
function get_day( $date ) { $tem = explode('-' , $date); //切割日期 得到年份和月份 $year = $tem['0']; $month = ...
- windows右键打开方式里面添加新的应用程序
1.打开注册表编辑器.打开运行窗口,快捷键,开始+R.输入“regedit”,回车确定. 2.进入注册表编辑器的HKEY_CLASSES_ROOT文件夹下的*子文件夹下的shell文件夹. 3.右键s ...
- 关闭PdfReader右侧工具栏的方法
1.首先单次关闭工具栏 点击视图-显示/隐藏-工具窗格,关闭右侧工具栏,但下次打开pdf还会出来,所以: 2.记住设置状态 编辑-首选项-文档-记住工具窗格当前状态
- leetcode554
public class Solution { public int LeastBricks(IList<IList<int>> wall) { ) { ; } ; Dicti ...
- 切面(Aspect)获取请求参数和返回值
@Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 接收到请求, ...