C# 调用系统默认打印机打印 以小票打印机为例
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
在C#编程中,调用系统默认打印机进行打印是一项常见的任务,尤其在开发POS系统或需要打印小票的场景下。本示例将详细介绍如何利用C#实现小票打印机的打印功能,包括加载系统字体、展示多种文本样式以及打印二维码。 我们需要引入必要的命名空间,如`System.Drawing`和`System.Drawing.Printing`,它们提供了处理图形和打印的基本类: ```csharp using System.Drawing; using System.Drawing.Printing; ``` 1. **获取系统默认打印机**: 可以通过`PrintDocument`类的`DefaultPrinter`属性获取默认打印机的名称: ```csharp string defaultPrinter = PrinterSettings.InstalledPrinters[0]; PrintDocument printDoc = new PrintDocument(); printDoc.PrinterSettings.PrinterName = defaultPrinter; ``` 2. **定义打印事件**: `PrintPageEventArgs`类包含了打印页面所需的所有信息。我们需定义`PrintPage`事件处理程序,以指定要打印的内容: ```csharp printDoc.PrintPage += new PrintPageEventHandler(PrintPageHandler); ``` 3. **实现打印逻辑**: 在`PrintPageHandler`方法中,我们将创建Graphics对象,用于在纸上绘制文本和图像: ```csharp private void PrintPageHandler(object sender, PrintPageEventArgs e) { // 加载系统字体 Font systemFont = new Font("宋体", 12); // 示例:使用宋体,大小12 // 设置文本样式 SolidBrush blackBrush = new SolidBrush(Color.Black); // 打印普通文本 e.Graphics.DrawString("欢迎光临", systemFont, blackBrush, new PointF(10, 10)); // 打印加粗文本 Font boldFont = new Font(systemFont, FontStyle.Bold); e.Graphics.DrawString("商品清单", boldFont, blackBrush, new PointF(10, 30)); // 清理资源 blackBrush.Dispose(); boldFont.Dispose(); } ``` 4. **打印二维码**: 我们需要一个库来解析和绘制二维码,例如`ZXing.Net`。安装NuGet包`ZXing.Net`后,可以创建一个`BarcodeWriter`对象,设置编码类型为QRCode,并使用Graphics对象绘制到页面上: ```csharp using ZXing; using ZXing.QrCode; // 创建二维码 BarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions { Width = 200, Height = 200 } }; Bitmap qrCodeBitmap = writer.Write("http://example.com"); // 在页面上绘制二维码 e.Graphics.DrawImage(qrCodeBitmap, new Point(10, 60)); qrCodeBitmap.Dispose(); ``` 5. **设置打印范围和页边距**: 如果需要控制打印范围,可以设置`PrintDocument`的`PageBounds`属性。页边距可以通过`Margins`属性设置: ```csharp printDoc.DefaultPageSettings.Margins = new Margins(50, 50, 50, 50); ``` 6. **开始打印**: 调用`Print`方法启动打印任务: ```csharp printDoc.Print(); ``` 7. **自定义小票格式**: 对于小票打印机,可能需要考虑纸张尺寸,例如热敏纸的宽度通常是58mm。可以自定义`PrintDocument`的纸张大小,确保内容适应打印机: ```csharp PaperSize ticketSize = new PaperSize("CustomTicket", 58 * 96, 100 * 96); // 96 dots/inch printDoc.DefaultPageSettings.PaperSize = ticketSize; ``` 通过以上步骤,我们已经成功实现了C#调用系统默认打印机打印小票的功能,包括加载系统字体、展示不同文本样式以及打印二维码。注意,实际应用中可能还需要处理更多的细节,如文本换行、对齐方式、二维码纠错级别等,以满足特定需求。
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/EXE.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/EXE.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/EXE.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
- 1
![avatar](https://profile-avatar.csdnimg.cn/fd3a8878cc1146f98378cac5549a7a40_acdyf.jpg!1)
- 粉丝: 5
- 资源: 13
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
- 3
前往页