POS58票据打印机走纸测试

1.打印文本

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    Font font = new Font("宋体", 12);
    Brush brush = Brushes.Black;
    e.Graphics.DrawString("要打印的文本", font, brush, 0, 0);
}



这种方法走纸正常,但文本显示不好控制(右边文本没有显示完)。

2.打印控件(Panel)

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    using (Bitmap bitmap = new Bitmap(panel1.Width, panel1.Height))
    {
        panel1.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
        e.Graphics.DrawImage(bitmap, 0, 0, e.Graphics.VisibleClipBounds.Width, e.Graphics.VisibleClipBounds.Height);
    }
}

不要去测试这段代码,我测试时纸走了2米多还在走-_-#,于是做了下改进:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    using (Bitmap bitmap = new Bitmap(panel1.Width, panel1.Height))
    {
        panel1.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
        int width = (int)e.Graphics.VisibleClipBounds.Width;
        int height = width * bitmap.Height / bitmap.Width;
        e.Graphics.DrawImage(bitmap, 0, 0, width, height);
    }
}



打印成功。注意,上面代码用e.Graphics.VisibleClipBounds获取打印区域,不要用e.PageBounds,我被网上一篇文章误导用了它,结果打印总有问题被折腾了很久。

参考资料

[1].PrintPageEventArgs类:http://msdn.microsoft.com/zh-cn/library/system.drawing.printing.printpageeventargs(v=vs.80).aspx

评论: 0 | 引用: 0 | 查看次数: 3470
发表评论
登录后再发表评论!