带有嵌入字体的 Winform .net PrintDocument DrawString 不起作用

问题描述

在谷歌搜索了很多次之后,我来这里寻求帮助: 问题:当我尝试使用 printDocument 绘制时,代码显示认字体(Arial)。 请帮忙。

enter image description here

namespace EmbededFonts
{
    public partial class Form1 : Form
    {

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont,uint cbFont,IntPtr pdv,[System.Runtime.InteropServices.In] ref uint pcFonts);

        private PrivateFontCollection fonts = new PrivateFontCollection();

        Font mySignatureFont;

        public Form1()
        {
            InitializeComponent();

            byte[] fontData = Properties.Resources.SignatureFont;
            IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
            System.Runtime.InteropServices.Marshal.copy(fontData,fontPtr,fontData.Length);
            uint dummy = 0;
            fonts.AddMemoryFont(fontPtr,Properties.Resources.SignatureFont.Length);
            AddFontMemResourceEx(fontPtr,(uint)Properties.Resources.SignatureFont.Length,IntPtr.Zero,ref dummy);
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);

            mySignatureFont = new Font(fonts.Families[0],16.0F);

        } 

        private void Form1_Paint(object sender,PaintEventArgs e)
        {
            e.Graphics.DrawString("My Artistic Signature",mySignatureFont,Brushes.Black,10,10);
        }

        private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawString("My Artistic Signature",10);
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)