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]);//图1
picQRcode.Image = map1;
}
else if (fidList.Count == 2)//打印2个
{
Bitmap map1 = CreateQRcode(fidList[0]);//图1
Bitmap map2 = CreateQRcode(fidList[1]);//图2
picQRcode.Image = StitchingImages(map1, map2);//图1+图2
}
else if (fidList.Count == 3)//打印3个,最多打印3个
{
Bitmap map1 = CreateQRcode(fidList[0]);//图1
Bitmap map2 = CreateQRcode(fidList[1]);//图2
Bitmap map3 = CreateQRcode(fidList[2]);//图3
Bitmap temp1 = StitchingImages(map1, map2);//图1+图2=图4
Bitmap temp2 = StitchingImages(temp1, map3);//图4+图3
picQRcode.Image = temp2;//图1+图2+图3
}
#endregion
Print();//打印二维码
}
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);
}
}
}
打印机设置:
效果图: