3 Star 27 Fork 1.1K

独立观察员/DotNetCodes

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FontHelper.cs 5.54 KB
一键复制 编辑 原始数据 按行查看 历史
独立观察员 提交于 2021-06-03 10:43 +08:00 . [dlgcy]安装WPF资源文件中的字体
using System;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace DotNet.Utilities
{
/// <summary>
/// 字体帮助类
/// </summary>
public class FontHelper
{
#region 系统API
[DllImport("kernel32.dll", SetLastError = true)]
static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString);
[DllImport("gdi32")]
public static extern int AddFontResource(string lpFileName);
#endregion
/// <summary>
/// 检测某种字体样式是否可用
/// </summary>
/// <param name="familyName">字体名称(如:"苹方 常规")</param>
/// <param name="fontStyle">字体样式</param>
/// <returns>是否安装了该字体</returns>
public static bool CheckFont(string familyName, FontStyle fontStyle = FontStyle.Regular)
{
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
FontFamily[] fontFamilies = installedFontCollection.Families;
foreach (var item in fontFamilies)
{
if (item.Name.Equals(familyName))
{
return item.IsStyleAvailable(fontStyle);
}
}
return false;
}
/// <summary>
/// 安装字体
/// </summary>
/// <param name="fontFilePath">字体文件全路径</param>
/// <returns>是否成功安装字体</returns>
/// <exception cref="UnauthorizedAccessException">不是管理员运行程序</exception>
/// <exception cref="Exception">字体安装失败</exception>
public static bool InstallFont(string fontFilePath)
{
try
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) == false)
{
throw new UnauthorizedAccessException("当前用户无管理员权限,无法安装字体");
}
//获取Windows字体文件夹路径
string fontPath = Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), "fonts", Path.GetFileName(fontFilePath));
//检测系统是否已安装该字体
if (!File.Exists(fontPath))
{
//将某路径下的字体拷贝到系统字体文件夹下
File.Copy(fontFilePath, fontPath); //font是程序目录下放字体的文件夹
AddFontResource(fontPath);
//安装字体
WriteProfileString("fonts", Path.GetFileNameWithoutExtension(fontFilePath) + "(TrueType)", Path.GetFileName(fontFilePath));
}
}
catch (Exception ex)
{
Console.WriteLine($"安装字体异常:{ex}");
return false;
}
return true;
}
/// <summary>
/// [dlgcy]安装WPF资源文件中的字体
/// </summary>
/// <param name="resourceUrl">WPF左右路径字符串(如:"/Main.ViewCore;component/Themes/Resources/Fonts/PingFang Regular.ttf")</param>
/// <returns>是否成功</returns>
public static bool InstallResoruceFont(string resourceUrl)
{
try
{
string fontPath = "font.ttf";
using (Stream stream = System.Windows.Application.GetResourceStream(new Uri(resourceUrl, UriKind.Relative)).Stream)
using (FileStream fs = new FileStream(fontPath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fs);
}
InstallFont(fontPath);
}
catch (Exception ex)
{
Console.WriteLine($"安装字体异常:{ex}");
return false;
}
return true;
}
/// <summary>
/// 使用内存中的字体,无安装无释放
/// </summary>
/// <param name="bytes">内存中的字体数据</param>
/// <returns>字体</returns>
public static Font GetResoruceFont(byte[] bytes)
{
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
IntPtr MeAdd = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, MeAdd, bytes.Length);
pfc.AddMemoryFont(MeAdd, bytes.Length);
return new Font(pfc.Families[0], 15, FontStyle.Regular);
}
/// <summary>
/// 通过文件获取字体
/// </summary>
/// <param name="filePath">文件全路径</param>
/// <returns>字体</returns>
public static Font GetFontByFile(string filePath)
{
//程序直接调用字体文件,不用安装到系统字库中。
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
pfc.AddFontFile(filePath);//字体文件的路径
Font font = new Font(pfc.Families[0], 24, FontStyle.Regular, GraphicsUnit.Point, 0);//font就是通过文件创建的字体对象
return font;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/dlgcy/dotnetcodes.git
git@gitee.com:dlgcy/dotnetcodes.git
dlgcy
dotnetcodes
DotNetCodes
cd6d091d3c082ec1f7f450b0e4aec61c6a1ea5cd

搜索帮助