嵌入字体的Winform.net PrintDocument抽绳无效

嵌入字体的Winform.net PrintDocument抽绳无效,.net,winforms,printing,fonts,gdi+,.net,Winforms,Printing,Fonts,Gdi+,在谷歌进行了多次搜索之后,我来到这里寻求帮助: 问题:当我尝试使用printDocument绘制时,代码显示默认字体(Arial)。 请帮忙 删除这个System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr)并在Form1.Closed事件中处理fonts(PrivateFontCollection对象)和mySignatureFont。看起来您正在使用PrintPreviewControl。使用其Paint事件使用该字体绘制字符

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


删除这个
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr)并在
Form1.Closed
事件中处理
fonts
(PrivateFontCollection对象)和
mySignatureFont
。看起来您正在使用PrintPreviewControl。使用其Paint事件使用该字体绘制字符串,而不是PrintDocument.PrintPage事件。顺便说一句,请阅读此处的注释:如果您想从文件(在任何情况下都是字节数组)加载字体,请选择您喜欢的代码版本。
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, 0, 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", mySignatureFont, Brushes.Black, 10, 10);
        }
    }
}