码迷,mamicode.com
首页 > Windows程序 > 详细

[转]将字体嵌入程序资源中 C# Winform

时间:2014-06-13 14:59:25      阅读:499      评论:0      收藏:0      [点我收藏+]

标签:winform   style   class   blog   code   java   

bubuko.com,布布扣
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        PrivateFontCollection pfc = new PrivateFontCollection();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string fontName = "WindowsFormsApplication2.汉仪水波体简.ttf";

            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream stream = assembly.GetManifestResourceStream(fontName);
            
            byte[] fontData = new byte[stream.Length];

            stream.Read(fontData, 0, (int)stream.Length);
            stream.Close();

            unsafe
            {

                fixed (byte* pFontData = fontData)
                {
                    pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length);
                }
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font font = new Font(pfc.Families[0], 20);
            g.DrawString("测试文字", font, new SolidBrush(Color.Black), 200, 100);
        }
    }
}
bubuko.com,布布扣

 

[转]将字体嵌入程序资源中 C# Winform,布布扣,bubuko.com

[转]将字体嵌入程序资源中 C# Winform

标签:winform   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/z5337/p/3785131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!