C# 如何实现打印图片

一、前言

        图片是指由图形、图像等构成的平面媒体,它有多种格式包括JPG、PNG、BMP、GIF等。我们一直往信息数字化方向发展,但打印在我的业务中还会有一些存在。在某些场景下我们需要将图片通过打印机打印出来,例如图片类型的快递面单、项目系统图片附件等。今天文章主要介绍C#提供的打印类库PrintDocument 是如何实现打印图片的,希望对大家有所帮助。

二、详细

.NET的C#语言为打印提供了一组类库都归属在System.Printing.dll中,包括 PrintDocument、PrintServer、PrintQueue、LocalPrintServer等。下图是C#用PrintDocument打印会用到的一些其它类,今天先来了解PrintDocument。

C# 如何实现打印图片_第1张图片


属性

        下表只列出打印时常用的一些属性:

名称 描述
DefaultPageSettings 获取或设置用作要打印的所有页的默认设置的页设置。
DocumentName 获取或设置打印文档时要显示的文档名称(例如,在打印状态对话框或打印机队列中)。
OriginAtMargins 获取或设置一个值,该值指示与页关联的图形对象的位置是位于用户指定边距内,还是位于该页可打印区域的左上角。
PrintController 获取或设置指导打印进程的打印控制器。
PrinterSettings 获取或设置对文档进行打印的打印机。

方法

        下表只列出打印时常用的一些方法:

名称 描述
OnBeginPrint() 引发 BeginPrint 事件, 该事件在调用 Print() 方法之后并在打印文档的第一页之前被调用。
OnEndPrint() 引发 EndPrint 事件,该事件在文档的最后一页打印完后被调用。
OnPrintPage() 引发 PrintPage 事件,该事件在某页打印之前被调用。
OnQueryPageSettings() 引发 QueryPageSettings 事件。该事件正好在每个 PrintPage 事件之前被调用。
Print() 开始打印文档。

事件

        下表只列出打印时常用的一些方法:

名称 描述
BeginPrint 在调用 Print() 方法之后、打印文档首页之前发生。
EndPrint 打印完文档的最后一页时发生。
PrintPage 当需要为当前页打印的输出时发生。
QueryPageSettings 使用 QueryPageSettings 事件修改打印机设置。

三、例子

        我们在应用PrintDocument 来打印图片时,首先创建类的PrintDocument实例、设置 和 PrinterSettings等DocumentName属性,并调用 Print 方法来启动打印过程。下面的例子是如何通过编写C#代码来打印图片,通过代码能对PrintDocument有更深的了解,提供开发打印提供思路。

定义一个枚举:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fountain.Net.Core.Print
{
    /// 
    /// 打应结果状态
    /// 
    public enum PrintStatus
    {
        /// 
        /// 
        /// 
        PRINT_INIT = 0,
        /// 
        /// 
        /// 
        PRINT_FINISH= 1,
        /// 
        /// 
        /// 
        PRINT_CANCE = 2,
        /// 
        /// 
        /// 
        PRINT_ERROR = 3,
        /// 
        /// 
        /// 
        PRINTING = 4
    }
}

将打印图片封装在一个类中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Fountain.Net.Core.Print
{
    /// 
    /// 打印图片
    /// 
    public class PrintImage
    {
        /// 
        /// 图片文档
        /// 
        public Image ImageDocument { get; set; }
        /// 
        /// 打印结果
        /// 
        public PrintStatus Result { get; set; }
        /// 
        /// 打印机
        /// 
        public string PrinterName { get; set; }
        /// 
        /// 
        /// 
        public PrintImage()
        {
        }
        /// 
        /// 打印
        /// 
        public void Print()
        {
            try
            {
                // PrintDocument实例
                PrintDocument printDocument = new PrintDocument();
                // 开始打印事件
                printDocument.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.PrintDocument_BeginPrint);
                // 结束打印事件
                printDocument.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.PrintDocument_EndPrint);
                // 将事件处理函数添加到PrintPage中
                printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintDocument_PrintPage);
                //设置打印机
                printDocument.PrinterSettings.PrinterName = this.PrinterName;
                // 
                printDocument.DefaultPageSettings.Landscape = false;
                // 注册PrintPage事件,打印每一页时会触发该事件
                printDocument.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
                // 标准静默打应
                PrintController printController = new StandardPrintController();
                printDocument.PrintController = printController;
                // 打印对话框
                PrintDialog printDialog = new PrintDialog()
                {
                    // 打印文档
                    Document = printDocument,
                    AllowSomePages = true,
                    ShowHelp = true
                };
                printDocument.Print();
            }
            catch
            {

            }
        }
        /// 
        /// 预览
        /// 
        public void Preview()
        {
            try
            {
                // PrintDocument实例
                PrintDocument printDocument = new PrintDocument();
                // 开始打印事件
                printDocument.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.PrintDocument_BeginPrint);
                // 结束打印事件
                printDocument.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.PrintDocument_EndPrint);
                // 将事件处理函数添加到PrintPage中
                printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintDocument_PrintPage);
                // 打印方向
                printDocument.DefaultPageSettings.Landscape = false;
                // 注册PrintPage事件,打印每一页时会触发该事件
                printDocument.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
                // 打印预览对话框
                PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog()
                {
                    Document = printDocument,
                    TopLevel = true,
                };
                printPreviewDialog.ShowDialog();
            }
            catch
            {

            }
        }
        /// 
        /// 打印触发事件
        /// 
        private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {         
                if (this.ImageDocument!=null)
                {
                    //图片抗锯齿
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    //按物理尺寸打印
                    e.Graphics.DrawImage(this.ImageDocument, 0, 0); 
                    int width = this.ImageDocument.Width;
                    int height = this.ImageDocument.Height;
                    if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
                    {
                        width = e.MarginBounds.Width;
                        height = ImageDocument.Height * e.MarginBounds.Width / ImageDocument.Width;
                    }
                    else
                    {
                        height = e.MarginBounds.Height;
                        width = ImageDocument.Width * e.MarginBounds.Height / ImageDocument.Height;
                    }
                    e.HasMorePages = false;
                }
            }
            catch (Exception ex) 
            {
                Result=PrintStatus.PRINT_ERROR;
                throw new Exception(ex.Message);
            }
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        private void PrintDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            Result = PrintStatus.PRINTING;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        private void PrintDocument_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            Result = PrintStatus.PRINT_FINISH;
        }
    }
}

如何调用:


Stream imageStream = newFileStream(@"D:\Desktop\22559so24011740049.png", FileMode.Open, FileAccess.Read);
System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream);
PrintImage printImage = new PrintImage();
printImage.PrinterName = "Microsoft Print to PDF";
printImage.ImageDocument = image;
printImage.Print();

示例代码:C#实现打印图片功能代码例子资源-CSDN文库icon-default.png?t=N7T8https://download.csdn.net/download/Funniyuan/88804405?spm=1001.2014.3001.5503

四、小结

        文章对打印用到的类库进行了简单的介绍,通过如何打印图片的案例来应用它。大家也可以想想如何将PDF文件从打印机打出、打印表格文本以及在图片上写文本再输出等。


注意:纸张大小等例子中都是用默认计算机上打印机首选项的设置,可以通过PrinterSettings来设置。

你可能感兴趣的:(c#,.net)