winfrom生成二維碼,參考:https://blog.csdn.net/djk8888/article/details/87859089
 拼接兩張圖片,參考原文:https://blog.csdn.net/yanxiaozai/article/details/83148417 (感謝!)
 簡單打印圖片,參考原文:https://www.cnblogs.com/wuhuisheng/archive/2011/11/09/2243055.html (感謝!)
 給圖片添加水印(標簽),原文:http://blog.sina.com.cn/s/blog_6f7a7fb5010170ah.html (感謝!)
 
需求描述:斑馬Gk888t打印機,打印二維碼,一次打印打1~3張二維碼(一行最多3個)
 思路:把2~3張二維碼的圖片,拼接在一張圖上,打印該圖即可
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;namespace JYMS
{public partial class Frm3In1FixedAssetsQr : Form{public List<string> fidList;public Frm3In1FixedAssetsQr(){InitializeComponent();}private void Frm3In1FixedAssetsQr_Load(object sender, EventArgs e){CreateQRcode();}//批量生成二維碼private void CreateQRcode(){if (fidList != null && fidList.Any()){try{#region 生成二維碼if (fidList.Count == 1)//打印1個,至少打印1個{Bitmap map1 = CreateQRcode(fidList[0]);//圖1picQRcode.Image = map1;}else if (fidList.Count == 2)//打印2個{Bitmap map1 = CreateQRcode(fidList[0]);//圖1Bitmap map2 = CreateQRcode(fidList[1]);//圖2picQRcode.Image = StitchingImages(map1, map2);//圖1+圖2}else if (fidList.Count == 3)//打印3個,最多打印3個{Bitmap map1 = CreateQRcode(fidList[0]);//圖1Bitmap map2 = CreateQRcode(fidList[1]);//圖2Bitmap map3 = CreateQRcode(fidList[2]);//圖3Bitmap temp1 = StitchingImages(map1, map2);//圖1+圖2=圖4Bitmap temp2 = StitchingImages(temp1, map3);//圖4+圖3picQRcode.Image = temp2;//圖1+圖2+圖3}#endregionPrint();//打印二維碼}catch (Exception ex)//一般是字符串超出長度的極限報錯:索引超出了數組的極限{MessageBox.Show(ex.ToString());}}else{DialogResult = DialogResult.No;}}/// <summary>/// 生成二維碼/// </summary>/// <param name="fid">固定資產號</param>public Bitmap CreateQRcode(string fid){QRCodeEncoder Encoder = new QRCodeEncoder();Encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//二維碼類型Encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;//容錯程度(H:高)Encoder.QRCodeScale = 2;//像素點大小,1~3推薦Encoder.QRCodeVersion = 0;//0:自動判斷字符串長度,有最大的極限,太長會報錯//關鍵,用QRCodeEncoder內部Encode加密Bitmap image = Encoder.Encode("http://192.168.8.88:8026/FixedAssets/items.aspx?fid=" + fid);//本地測試MemoryStream MStream = new System.IO.MemoryStream();image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);//return image;return Watermark(image, "資產號:" + fid.PadLeft(6,'0'));//添加文字水印}/// <summary>/// 圖片底部添加水印:固定資產編號/// </summary>/// <param name="map1">原圖</param>/// <param name="text">文字</param>/// <param name="space">水印高度</param>/// <returns></returns>public Bitmap Watermark(Bitmap map1, String text, int space = 15){Image img1 = map1;var width = img1.Width;var height = img1.Height + space;//新圖片高度=原圖片高度+水印高度Bitmap bitMap = new Bitmap(width, height);Graphics g = Graphics.FromImage(bitMap);g.FillRectangle(Brushes.White, new Rectangle(0, 0, width, height));g.DrawImage(map1, 0, 0, img1.Width, img1.Height);Font font = new Font("微軟雅黑", 8);//文字字體,大小Brush brush = new SolidBrush(Color.Black);//文字顏色(黑色)g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;g.DrawString(text, font, brush, 0, img1.Height + 1, StringFormat.GenericDefault);//添加文字MemoryStream MStream = new System.IO.MemoryStream();bitMap.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);return bitMap;}/// <summary>/// 拼接圖片/// </summary>/// <param name="map1">圖片1</param>/// <param name="map2">圖片2</param>/// <param name="space">間隔留空大小</param>/// <returns></returns>public Bitmap StitchingImages(Bitmap map1, Bitmap map2, int space = 38){Image img1 = map1;Image img2 = map2;#region 這個是上下拼接//var width = Math.Max(img1.Width, img2.Width);//var height = img1.Height + img2.Height + space;#endregion#region 這個是左右拼接var width = img1.Width + img2.Width + space;var height = Math.Max(img1.Height, img2.Height);#endregion// 初始化畫布(最終的拼圖畫布)并設置寬高Bitmap bitMap = new Bitmap(width, height);// 初始化畫板Graphics g1 = Graphics.FromImage(bitMap);// 將畫布涂為白色(底部顏色可自行設置)g1.FillRectangle(Brushes.White, new Rectangle(0, 0, width, height));//在x=0,y=0處畫上圖一g1.DrawImage(map1, 0, 0, img1.Width, img1.Height);//在x=0,y在圖一往下10像素處畫上圖二//g1.DrawImage(map2, 0, img1.Height + space, img2.Width, img2.Height);//這個是上下拼圖g1.DrawImage(map2, img1.Width + space, 0, img2.Width, img2.Height);//這個是左右拼圖map1.Dispose();map2.Dispose();//Image img = bitMap;//保存return bitMap;}//直接打印,不彈出打印機對話框public void Print(){PrintDialog MyPrintDg = new PrintDialog();//MyPrintDg.Document = printDocument1;//if (MyPrintDg.ShowDialog() == DialogResult.OK)//{try{printDocument1.Print();DialogResult = DialogResult.OK;//關閉彈窗}catch (Exception ex){printDocument1.PrintController.OnEndPrint(printDocument1, new System.Drawing.Printing.PrintEventArgs());//停止打印DialogResult dr = MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK);if (dr == DialogResult.OK){DialogResult = DialogResult.OK;//關閉彈窗}}//}}private void btnPrintQr_Click(object sender, EventArgs e){Print();}private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e){e.Graphics.DrawImage(picQRcode.Image, 0, 0);}}
} 
打印機設置:
 
 效果圖:
 
 
                            總結
                            
                                以上是生活随笔為你收集整理的winfrom实现,斑马Gk888t打印机,连续打印二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。