c# 怎么使用资源字体?
但是不知道该怎么使用这些字体,请高手指教!! 展开
你可以直接从资源文件里把它读出来或者,把资源文件里的字体文件保存到本地,然后在加载,如下Runic和Torchlight_Regular是字体文件
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing.Text;
private void button1_Click(object sender, EventArgs e)
{
GCHandle hObject = GCHandle.Alloc(Properties.Resources.Runic, GCHandleType.Pinned);
IntPtr intptr = hObject.AddrOfPinnedObject();
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddMemoryFont(intptr, Properties.Resources.Runic.Length);//从内存加载
File.WriteAllBytes("D:\\Torchlight_Regular.ttf", Properties.Resources.Torchlight_Regular);
privateFontCollection.AddFontFile("D:\\Torchlight_Regular.ttf");//从文件加载
Font font1 = new Font(privateFontCollection.Families[0], 20);
Font font2 = new Font(privateFontCollection.Families[1], 20);
Graphics g = this.CreateGraphics();
g.DrawString("Hello World", font1, Brushes.Black, new PointF(10, 10));
g.DrawString("Hello World", font2, Brushes.Black, new PointF(10, 40));
g.Dispose();
font1.Dispose();
font2.Dispose();
if (hObject.IsAllocated)
hObject.Free();
privateFontCollection.Dispose();
}