C#调用打印机,打印图片

PrintPreviewDialog是打印预览对话框,需要传一个 printDocument给它才可以显示该对话框

PrintDocument是具体要打印的内容,可以是图片,也可以是加载文档,文字。


代码如下:

//打印
        private void button5_Click(object sender, EventArgs e)
        {
            //获取或设置一个值,该值指示是否发送到文件或端口
            printDocument1.PrinterSettings.PrintToFile = true;
            //设置打印时横向还是纵向
            printDocument1.DefaultPageSettings.Landscape = true;
            //打印预览
           // PrintPreviewDialog ppd = new PrintPreviewDialog();
            if (bmp == null)
            {
                return;
            }
 
            //设置边距
            Margins margin = new Margins(20, 20, 20, 20);
            printDocument1.DefaultPageSettings.Margins = margin;
            ////纸张设置默认
            //PaperSize pageSize = new PaperSize("First custom size", 800, 600);
            //pd.DefaultPageSettings.PaperSize = pageSize;
            //打印事件设置
            printDocument1.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
           //ppd.Document = printDocument1;
           // ppd.ShowDialog();
            try
            {
                printDocument1.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                printDocument1.PrintController.OnEndPrint(printDocument1, new PrintEventArgs());
            }
 
        }
         
        //打印事件处理
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = bmp.Width;
            int height = bmp.Height;
            Rectangle destRect = new Rectangle(x, y, width, height);
            e.Graphics.DrawImage(bmp, destRect, 0, 0, bmp.Width, bmp.Height, System.Drawing.GraphicsUnit.Pixel);
        }
相关文章
相关标签/搜索