窗体设计显示图
一,生成二维码
/// <summary>/// 生成二维码/// </summary>/// <param name="text">内容</param>/// <param name="width">宽度</param>/// <param name="height">高度</param>/// <returns></returns>public static Bitmap Generate1(string text, int width, int height){if (string.IsNullOrEmpty(text)){return null;}BarcodeWriter writer = new BarcodeWriter();writer.Format = BarcodeFormat.QR_CODE;QrCodeEncodingOptions options = new QrCodeEncodingOptions(){DisableECI = true,//设置内容编码CharacterSet = "UTF-8", //设置二维码的宽度和高度Width = width,Height = height,Margin = 1//设置二维码的边距,单位不是固定像素};writer.Options = options;Bitmap map = writer.Write(text);return map;}
二,调用
/// <summary>/// 生成二维码/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){if (string.IsNullOrEmpty(textBox2.Text)){MessageBox.Show("请输入要生成二维码的数据");}pictureBox2.Image = BarcodeHelper.Generate1(textBox2.Text, 160, 140);}
三,二维码打印
[DllImport("TSCLIB.dll", EntryPoint = "about")]public static extern int about();[DllImport("TSCLIB.dll", EntryPoint = "openport")]public static extern int openport(string printername);[DllImport("TSCLIB.dll", EntryPoint = "barcode")]public static extern int barcode(string x, string y, string type,string height, string readable, string rotation,string narrow, string wide, string code);[DllImport("TSCLIB.dll", EntryPoint = "clearbuffer")]public static extern int clearbuffer();[DllImport("TSCLIB.dll", EntryPoint = "closeport")]public static extern int closeport();[DllImport("TSCLIB.dll", EntryPoint = "downloadpcx")]public static extern int downloadpcx(string filename, string image_name);[DllImport("TSCLIB.dll", EntryPoint = "formfeed")]public static extern int formfeed();[DllImport("TSCLIB.dll", EntryPoint = "nobackfeed")]public static extern int nobackfeed();[DllImport("TSCLIB.dll", EntryPoint = "printerfont")]public static extern int printerfont(string x, string y, string fonttype,string rotation, string xmul, string ymul,string text);[DllImport("TSCLIB.dll", EntryPoint = "printlabel")]public static extern int printlabel(string set, string copy);[DllImport("TSCLIB.dll", EntryPoint = "sendcommand")]public static extern int sendcommand(string printercommand);[DllImport("TSCLIB.dll", EntryPoint = "setup")]public static extern int setup(string width, string height,string speed, string density,string sensor, string vertical,string offset);[DllImport("TSCLIB.dll", EntryPoint = "windowsfont")]public static extern int windowsfont(int x, int y, int fontheight,int rotation, int fontstyle, int fontunderline,string szFaceName, string content);//打印二维码public static void printQrCode(string barcode){//TSCLIB_DLL.about(); //Show the DLL versionBarcodeHelper.openport("TSC TTP-342 PRO"); //Open specified printer driverBarcodeHelper.setup("77.5", "50", "2", "15", "0", "6", "0"); //Setup the media size and sensor type infoBarcodeHelper.clearbuffer();BarcodeHelper.sendcommand("DIRECTION 0");BarcodeHelper.windowsfont(137, 305, 40, 0, 0, 0, "宋体", barcode);BarcodeHelper.sendcommand("QRCODE 212,58,L,10,A,0,M2,S7,\"" + barcode + "\"" + "\r");BarcodeHelper.printlabel("1", "1"); //Print labelsBarcodeHelper.closeport();}//关闭打印机端口public static void closeportExt(){BarcodeHelper.closeport();}
四,调用打印
/// <summary>/// 打印二维码/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){/* if (string.IsNullOrEmpty(textBox2.Text)){MessageBox.Show("请输入要生成二维码的数据");}*/if (textBox2.Text.Trim() == ""){MessageBox.Show("没有信息,请输入数据", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);return;}try{BarcodeHelper.printQrCode(textBox2.Text.Trim());}catch (Exception ex){System.Diagnostics.Debug.WriteLine(ex.Message);}}
需要添加引用,VS上可以直接生成,代码可以直接使用。