当前位置: 首页 > news >正文

Winform实现条码打印

1、首先需要包含头文件

using ZXing;
using ZXing.Common;

2、根据规则生成条码图片

private Bitmap CreateBarCode(string barCode,int width,int height){try{              // 条形码类型(此处以EAN_13为例)//BarcodeFormat barcodeFormat = BarcodeFormat.EAN_13;BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;// 条形码的输出文件名string outputFilePath = "output_barcode.png";// 创建条形码编码器实例BarcodeWriter barcodeWriter = new BarcodeWriter{// 设置条形码编码格式Format = barcodeFormat,// 设置条形码的大小(宽度和高度,单位像素)//Options = new ZXing.Common.EncodingOptions { Width = 300, Height = 100 },Options = new EncodingOptions{Width = width,Height = height,Margin = 10 // 设置边距以避免锯齿//PureBarcode = false //是否是纯码,如果为 false,则会在图片下方显示数字}// 可选:设置条形码的颜色(前景色为黑色,背景色为白色)// Options.ForegroundColor = Color.Black;// Options.BackgroundColor = Color.White;};// 生成条形码图像Bitmap barcodeBitmap = barcodeWriter.Write(barCode);var barcodeImage = ResizeBarcode(barcodeBitmap, barcodeWidth / 10, barcodeHeight / 10); // 保存条形码到文件barcodeImage.Save(outputFilePath);return barcodeImage;}catch (Exception ex){MessageBox.Show(ex.Message.ToString());return null;}}public Bitmap ResizeBarcode(Bitmap original, int widthMm, int heightMm){float dpi = printResolution; // 假设打印机分辨率为300 DPIfloat widthPx = widthMm * dpi / 25.4f; // 将毫米转换为像素,25.4mm = 1英寸float heightPx = heightMm * dpi / 25.4f; // 同上return new Bitmap(original, new Size((int)widthPx, (int)heightPx));}


3、打印条码

 private void BarPrint(Bitmap barcodeBitmap){try{PrintDocument pd = new PrintDocument();pd.PrintPage += (sender, e) =>{//使绘图质量最高,即消除锯齿e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;e.Graphics.CompositingQuality = CompositingQuality.HighQuality;// 在打印页面上绘制条形码图像e.Graphics.DrawImage(barcodeBitmap, new Point(10, 10));};//预览功能//PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();//printPreviewDialog.Document = pd;//printPreviewDialog.ShowDialog();pd.Print();              }catch (Exception ex){MessageBox.Show(ex.Message.ToString());}}

相关文章:

  • Vue生命周期详细解析
  • AI语音助手自定义角色百度大模型 【全新AI开发套件掌上AI+4w字教程+零基础上手】
  • Android SDK 下载及配置 --- app笔记
  • 【分布式锁通关指南 09】源码剖析redisson之公平锁的实现
  • [KVM] KVM挂起状态恢复失败与KVM存储池迁移
  • Spring JDBC 的开发步骤(注解方式)
  • 私有知识库 Coco AI 实战(三):摄入 Elasticsearch 官方文档
  • Go语言学习笔记(一)
  • 【论文阅读】Dual-branch Cross-Patch Attention Learning for Group Affect Recognition
  • 代理模式:控制对象访问的中间层设计
  • 论文阅读 | 大模型工具调用控制的策略优化
  • Spark与Hadoop之间的联系与区别
  • 使用nodeJs的express+axios+cors做代理
  • 配置MambaIRv2: Attentive State Space Restoration的环境
  • Sql刷题日志(day5)
  • 说一下Redis的发布订阅模型和PipeLine
  • OpenBayes 一周速览|EasyControl 高效控制 DiT 架构,助力吉卜力风图像一键生成;TripoSG 单图秒变高保真 3D 模型
  • leetcode hot100尝试1
  • 零基础入门 Verilog VHDL:在线仿真与 FPGA 实战全流程指南
  • 鸿蒙中的并发线程间通信、线程间通信对象
  • 温氏股份一季度归母净利润20.01亿元,同比扭亏为盈
  • 解放日报:上海一季度GDP同比增长5.1%,两大新动能助推经济“开门红”
  • 广东江门公布“小客车坠海致3死”事故评估报告,司机被判三年缓五年
  • 校友伉俪捐赠10亿元!成立复旦大学学敏高等研究院
  • 浦江观察|3.6亿元消费券,为上海餐饮业带来了什么?
  • 印尼塔劳群岛发生6.2级地震,震源深度140千米