WinForm(C/S)项目中使用矢量字体(FontAwsome、Elegant)图标
1、介绍
字体图标在Web应用中最为常见,字体图标是矢量的,矢量图意味着每个图标都能在所有大小的屏幕上完美呈现,可以随时更改大小和颜色,而且不失真。字体图标常见的有Font Awesome和Elegant Icon Font,她们不仅图标数量多,还可以免费使用。这些图标如果能用在WinForm项目中,不仅可以带来更加直观的界面效果,也能让图标不再局限于类似png类型,本文将介绍在WinForm项目中如何使用字体图标。
2、字体图标的选择
网上IconFont资源很多,同时很多提供SVG下载的网站都会提供对应的IconFont文件。本文就以:比较流行且开源免费的FontAwesome字体图标为例,讲解.NET开发的WinForm项目如何使用。
FontAwesome,官网:https://fontawesome.com.cn/v4/icons

在上图中,我们可以看到每个图标都有对应的Unicode编码,我们需要使用这个编码来做图标的展示。
3、使用方法
下载字体图标到本地,放到项目相应的位置,如在我们的项目中使用了两类字体图标,FontAwesome和ElegantIcon,如下图所示。


在项目中定义字体编码对应的枚举部分代码如下所示。
/// <summary>
/// 图标枚举,包含Awesome图标和Elegant图标,分别以A和E开头
/// </summary>
public enum FontIcons
{
#region Awesome English:Awesome
/// <summary>
/// a fa 500PX
/// </summary>
A_fa_500px = 0xf26e,
/// <summary>
/// a fa address book
/// </summary>
A_fa_address_book = 0xf2b9,
/// <summary>
/// a fa address book o
/// </summary>
A_fa_address_book_o = 0xf2ba,
/// <summary>
/// a fa address card
/// </summary>
A_fa_address_card = 0xf2bb,
#endregion
#region Elegant English:Elegant
/// <summary>
/// The e arrow up
/// </summary>
E_arrow_up = 0x21,
/// <summary>
/// The e arrow down
/// </summary>
E_arrow_down = 0x22,
/// <summary>
/// The e arrow left
/// </summary>
E_arrow_left = 0x23,
#endregion
}
定义字体图标加载公共类:FontImagesHelper.cs,此类不仅支持对待加载图标指定尺寸大小、还可以设置前景色和背景色。

FontImagesHelper.cs源码如下:
/// <summary>
/// 字体图标图片,awesome字体默认加载,elegant字体在使用时延迟加载
/// </summary>
public static class FontImagesHelper
{
/// <summary>
/// The m font collection
/// </summary>
private static readonly PrivateFontCollection fontCollection = new PrivateFontCollection();
/// <summary>
/// The m fonts awesome
/// </summary>
private static readonly Dictionary<string, Font> fontsAwesome = new Dictionary<string, Font>();
/// <summary>
/// The m fonts elegant
/// </summary>
private static readonly Dictionary<string, Font> fontsElegant = new Dictionary<string, Font>();
/// <summary>
/// The m cache maximum size
/// </summary>
private static Dictionary<int, float> cacheMaxSize = new Dictionary<int, float>();
/// <summary>
/// The minimum font size
/// </summary>
private const int MinFontSize = 8;
/// <summary>
/// The maximum font size
/// </summary>
private const int MaxFontSize = 43;
/// <summary>
/// 构造函数
/// </summary>
/// <exception cref="FileNotFoundException">Font file not found</exception>
static FontImagesHelper()
{
string strFile = Path.Combine(SystemInfo.StartupPath, "Resource", "IconFont\\FontAwesome.ttf");
fontCollection.AddFontFile(strFile);
float size = MinFontSize;
for (int i = 0; i <= (MaxFontSize - MinFontSize) * 2; i++)
{
fontsAwesome.Add(size.ToString("F2"), new Font(fontCollection.Families[0], size, FontStyle.Regular, GraphicsUnit.Point));
size += 0.5f;
}
}
/// <summary>
/// 获取图标
/// </summary>
/// <param name="iconName">图标名称</param>
/// <param name="imageSize">图标大小</param>
/// <param name="foreColor">前景色</param>
/// <param name="backColor">背景色</param>
/// <returns>图标</returns>
public static Icon GetIcon(string iconName, int imageSize = 32, Color? foreColor = null, Color? backColor = null)
{
FontIcons icon = (FontIcons)Enum.Parse(typeof(FontIcons), iconName);
return GetIcon(icon, imageSize, foreColor, backColor);
}
/// <summary>
/// 获取图标
/// </summary>
/// <param name="iconName">图标名称</param>
/// <param name="imageSize">图标大小</param>
/// <param name="foreColor">前景色</param>
/// <param name="backColor">背景色</param>
/// <returns>图标</returns>
public static Icon GetIcon(FontIcons iconName, int imageSize = 32, Color? foreColor = null, Color? backColor = null)
{
try
{
Bitmap image = GetImage(iconName, imageSize, foreColor, backColor);
return image != null ? ToIcon(image, imageSize) : null;
}
catch
{
return null;
}
}
/// <summary>
/// 获取图标
/// </summary>
/// <param name="iconName">图标名称</param>
/// <param name="imageSize">图标大小</param>
/// <param name="foreColor">前景色</param>
/// <param name="backColor">背景色</param>
/// <returns>图标</returns>
public static Bitmap GetImage(string iconName, int imageSize = 32, Color? foreColor = null, Color? backColor = null)
{
try
{
FontIcons icon = (FontIcons)Enum.Parse(typeof(FontIcons), iconName);
return GetImage(icon, imageSize, foreColor, backColor);
}
catch
{
return null;
}
}
/// <summary>
/// Gets the size of the icon.
/// </summary>
/// <param name="iconName">The icon text.</param>
/// <param name="graphics">The graphics.</param>
/// <param name="font">The font.</param>
/// <returns>Size.</returns>
private static Size GetIconSize(FontIcons iconName, Graphics graphics, Font font)
{
string text = char.ConvertFromUtf32((int)iconName);
return graphics.MeasureString(text, font).ToSize();
}
/// <summary>
/// Converts to icon.
/// </summary>
/// <param name="srcBitmap">The source bitmap.</param>
/// <param name="size">The size.</param>
/// <returns>Icon.</returns>
/// <exception cref="ArgumentNullException">srcBitmap</exception>
private static Icon ToIcon(Bitmap srcBitmap, int size)
{
if (srcBitmap == null)
{
throw new ArgumentNullException("srcBitmap");
}
Icon icon;
using (MemoryStream memoryStream = new MemoryStream())
{
new Bitmap(srcBitmap, new Size(size, size)).Save(memoryStream, ImageFormat.Png);
Stream stream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(stream);
if (stream.Length <= 0L)
{
return null;
}
binaryWriter.Write((byte)0);
binaryWriter.Write((byte)0);
binaryWriter.Write((short)1);
binaryWriter.Write((short)1);
binaryWriter.Write((byte)size);
binaryWriter.Write((byte)size);
binaryWriter.Write((byte)0);
binaryWriter.Write((byte)0);
binaryWriter.Write((short)0);
binaryWriter.Write((short)32);
binaryWriter.Write((int)memoryStream.Length);
binaryWriter.Write(22);
binaryWriter.Write(memoryStream.ToArray());
binaryWriter.Flush();
binaryWriter.Seek(0, SeekOrigin.Begin);
icon = new Icon(stream);
stream.Dispose();
}
return icon;
}
}
在RDIFramework.NET框架产品中整合了字体图标的使用,框架模块的图标按字体图标进行了整合加载,如下图所示。

调用对应图标的代码。
var img = FontImagesHelper.GetImage("A_fa_address_card", 26, "#66B9BF");
除了上面提到的字体图标,我们还可以使用阿里巴巴矢量图标库,地址:https://www.iconfont.cn/
4、参考文章
RDIFramework.NET CS敏捷开发框架 V6.1发布(.NET6+、Framework双引擎、全网唯一)
.NET开发WinForm(C/S)项目整合三种服务访问模式(直连、WCF、WebAPI)
WinForm(C/S)项目中使用矢量字体(FontAwsome、Elegant)图标的更多相关文章
- 在android项目中使用FontAwesome字体
在android项目中使用FontAweSome图标集,可以方便的适配各种屏幕分辨率,不必在各种不同分辨率文件夹中新建资源文件.使用字体是一种很巧妙的方法,把图像以字体的方式呈现,这样以前设置为and ...
- 在vue项目中添加特殊字体
这里的特殊字体,指的是一般用户电脑未安装到本地的字体,要引入这样的字体,首先需要把字体文件下载下来. 就像上图这样的,ttf格式的,然后在项目里添加它. 然后我们在font.css里用@font-fa ...
- vue项目中使用特殊字体
项目开发中遇到要是有‘数字’字体的情况,样式如下 网上查了一下实现的方法很简单,而且具体的实现方式大致相同,可以参考以下几个链接: https://www.cnblogs.com/zhangnan35 ...
- vue项目中引入特殊字体
特殊字体指的是默认电脑中没有的 1.下载字体 2.新建文件夹font,把字体放进去 3.新建font.css 4.使用@font-face设置字体 @font-face { font-family: ...
- vue 项目中使用阿里巴巴矢量图标库iconfont
原文:https://www.jianshu.com/p/38262f18eee2 1.打开iconfont阿里巴巴官网https://www.iconfont.cn 2.新建项目(这样方便后期维护图 ...
- vue项目中使用阿里iconfont图标
在上一篇文章中介绍了如何在vue项目中使用vue-awesome,如果你想了解,请移步<vue项目中使用vue-awesome> 这里介绍一下vue项目中如何使用阿里的iconfont图标 ...
- (九)WebGIS中的矢量查询(针对AGS和GeoServer)
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 在第七章里我们知道了WebGIS中要素的本质是UICompo ...
- 谈谈项目中遇到的各种iOS7适配问题
由于我的项目要适配到iOS7.1, 而现在已经是9时代了,在实际工作中我也是遇到了各种奇葩的坑,所以我想尽快把遇到的iOS7适配问题和解决方案分享出来,以后这些东西可能就用处不大了. 1.字体问题 i ...
- Android中TextView设置字体
最近项目中出现把字体设置成宋体,微软雅黑,黑体,楷体等的需求; 度娘发现Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使 ...
- vue项目中引入iconfont
背景 对于前端而言,图标的发展可谓日新月异.从img标签,到雪碧图,再到字体图标,svg,甚至svg也有了类似于雪碧图的方案svg-sprite-loader.雪碧图没有什么好讲的了,只是简单地利用了 ...
随机推荐
- 借助 Flutter 顺畅地开发多平台应用
Flutter 已于近期发布了 Flutter 2,Flutter 和 Dart 的产品总监 Tim Sneath 在 2021 年三月上旬举办的 Flutter Engage 活动中表示,Flutt ...
- 线段树与二分操作 vases and flowers ——hdu 4614
操作1,的关键是找到第一只和最后一只空花瓶,完全可以利用二分法查找,找第一只花瓶可以在[X,N]内查找,第一个位置pos1,最后一只花瓶则在[POS1,N]中找,然后更新[POS1,POS2],全部置 ...
- Kubernetes Deployment控制器(二十)
前面我们学习了 ReplicaSet 控制器,了解到该控制器是用来维护集群中运行的 Pod 数量的,但是往往在实际操作的时候,我们反而不会去直接使用 RS,而是会使用更上层的控制器,比如我们今天要学习 ...
- ftrace options 中的irq-info
/sys/kernel/debug/tracing/options/irq_info 是 ftrace 中的一个选项,用于启用或禁用有关中断的详细信息的跟踪. options/irq_info 的具体 ...
- eBPF 概述:第 4 部分:在嵌入式系统运行
1. 前言 在本系列的第 1 部分和第 2 部分,我们介绍了 eBPF 虚拟机内部工作原理,在第 3 部分我们研究了基于底层虚拟机机制之上开发和使用 eBPF 程序的主流方式. 在这一部分中,我们将从 ...
- Java解析HJ212环保报文
Java解析HJ212环保报文 toMap方法对报文进行基础的解析 /** * HJ212报文转换为标准化的Map * @param str HJ212报文 * @return */ public s ...
- 在Lua中实现Rust对象的绑定
实现目标:能将Rust对象快速的映射到lua中使用,尽可能的简化使用. 功能目标 以struct HcTestMacro为例: 类型构建,在lua调用local val = HcTestMacro.n ...
- 【FAQ】HarmonyOS SDK 闭源开放能力 —Push Kit(4)
1.问题描述: 目前华为推送API使用的是v2或者v1版本,请问目前最新的鸿蒙next使用v3版本是否兼容v2或者v1,反过来将v2或者v1的api可以推送鸿蒙next的设备吗? 解决方案: v3接口 ...
- Paimon lookup store 实现
Lookup Store 主要用于 Paimon 中的 Lookup Compaction 以及 Lookup join 的场景. 会将远程的列存文件在本地转化为 KV 查找的格式. Hash htt ...
- hadoop运行原理
包括HDFS和Mapreduce两部分. 1)HDFS自动保存多个副本,移动计算.缺点是小文件存取占用namenode内存,写入只支持追加,不能随机修改. 它存储的逻辑空间称为block,文件的权限类 ...