核心代码: /// <summary> /// 打印一维码 /// </summary> /// <param name="codeText"></param> /// <param name="g"></param> /// <param name="pointX"></param> /// <param name="pointY"></param> /// <param name="width"></param> /// <param name="height"></param> public void PrintBarcode(string codeText, Graphics g, int pointX, int pointY, int width, int height) { C1BarCode code = new C1BarCode(); code.CodeType = CodeTypeEnum.Code128; code.Text = codeText; try { g.DrawImage(code.Image, pointX, pointY, width, height); } catch (Exception ex) { if (code.Image == null) { MessageBox.Show("需要打印的字段中存在空值,条码会出现残缺,请检查是否维护了某些需要打印的字段" + ex.Message + "\n Some Information Need To Be Printed Is null, Please Contact Maintain It"); } } } /// <summary> /// 图片压缩式生成二维码,***添 /// </summary> /// <param name="e">e</param> /// <param name="QrBarcodeInfo">条码扫描出的信息</param> /// <param name="mapSize_x">bitmap x坐标</param> /// <param name="mapSize_y">bitmap y坐标</param> /// <param name="Qrbarcode_x">条码 x坐标</param> /// <param name="Qrbarcode_y">条码 y坐标</param> /// <param name="Qrbarcode_size">条码 长宽</param> public void Print_Image_Qrcode(System.Drawing.Printing.PrintPageEventArgs e, string QrBarcodeInfo, int mapSize_x, int mapSize_y, int Qrbarcode_x, int Qrbarcode_y, int Qrbarcode_size) { DotNetBarcode qrCode = new DotNetBarcode(DotNetBarcode.Types.QRCode); qrCode.QRQuitZone = 0; //qrCode.PrintChar = true; Bitmap bitmap = new Bitmap(mapSize_x, mapSize_y); //50 * 4, 50 * 4 Graphics gp = Graphics.FromImage(bitmap); //取图片作为 Graphics qrCode.QRWriteBar(QrBarcodeInfo, 0, 0, 2, gp); //画出二维码,图片的大小为二维码大小 gp.Dispose(); //pictureBox1.Image = bitmap; e.Graphics.DrawImage(bitmap, Qrbarcode_x, Qrbarcode_y, Qrbarcode_size, Qrbarcode_size); //将二维码图片 打印出来,缩放 35*4, 35*4 } /// <summary> /// 打印二维码 /// </summary> /// <param name="codeText"></param> /// <param name="pointX"></param> /// <param name="pointY"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="g"></param> public void PrintBarcode_QrCode(string codeText, float pointX, float pointY, float width, float height, Graphics g) { DotNetBarcode qrCode = new DotNetBarcode(DotNetBarcode.Types.QRCode); qrCode.QRQuitZone = 0; qrCode.WriteBar(codeText, pointX, pointY, width, height, g); }
分享共进步,谢谢阅读!
时间: 2024-10-11 22:12:03