/// <summary> /// /// </summary> /// <param name="code"></param> /// <param name="size"></param> /// <returns></returns> public static Bitmap ToImage(string code, int size = 180) { BarcodeWriter writer = new BarcodeWriter(); QrCodeEncodingOptions qr = new QrCodeEncodingOptions() { CharacterSet = "UTF-8", ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H, Height = size, Width = size, }; writer.Options = qr; writer.Format = BarcodeFormat.QR_CODE; Bitmap bitmap = writer.Write(code); return bitmap; }
/// <summary> /// 生成二维码图片 /// </summary> /// <param name="contents">要生成二维码包含的信息</param> /// <param name="width">生成的二维码宽度(默认300像素)</param> /// <param name="height">生成的二维码高度(默认300像素)</param> /// <returns>二维码图片</returns> public static Bitmap GeneratorQrCodeImage(string contents, int width = 300, int height = 300) { if (string.IsNullOrEmpty(contents)) { return null; } EncodingOptions options = null; BarcodeWriter writer = null; options = new QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = width, Height = height, ErrorCorrection = ErrorCorrectionLevel.H, //控制二维码图片的边框 Margin = 0 }; writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options }; Bitmap bitmap = writer.Write(contents); return bitmap; }
private void btnEncode_Click_1(object sender, EventArgs e) { if (txtEncodeData.Text.Trim() == String.Empty) { MessageBox.Show("请输入要生成二维码的内容!"); return; } // 1.设置QR二维码的规格 ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions(); qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码 int scale = Convert.ToInt16(txtSize.Text); qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码 qrEncodeOption.Height = scale; qrEncodeOption.Width = scale; qrEncodeOption.Margin = 1; // 设置周围空白边距 int version = Convert.ToInt16(cboVersion.Text); //选择编码修正方式,修正百分比越高内容越精确 string errorCorrect = cboCorrectionLevel.Text; if (errorCorrect == "L") { qrEncodeOption.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.L;//水平 7% 的字码可被修正 } else if (errorCorrect == "M") { qrEncodeOption.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.M;//水平 15% 的字码可被修正 } else if (errorCorrect == "Q") { qrEncodeOption.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.Q;//水平 25% 的字码可被修正 } else if (errorCorrect == "H") { qrEncodeOption.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H;//水平 30% 的字码可被修正 } // 2.生成条形码图片并保存 ZXing.BarcodeWriter wr = new BarcodeWriter(); wr.Format = BarcodeFormat.QR_CODE; // 二维码 wr.Options = qrEncodeOption; Bitmap img = wr.Write(this.txtEncodeData.Text.Trim()); picEncode.Image = img; }
private void btnEncode_Click(object sender, EventArgs e) { var txt = txtEncodeInfo.Text; if (string.IsNullOrEmpty(txt)) return; if (ckbEncode.Checked) { txt = Person.Encrypt(txt); } var size = 300; BarcodeWriter writer = new BarcodeWriter(); QrCodeEncodingOptions qr = new QrCodeEncodingOptions() { CharacterSet = "UTF-8", ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H, Height = size, Width = size }; writer.Options = qr; writer.Format = BarcodeFormat.QR_CODE; //txt += "|" + DateTime.Now.Ticks; Bitmap map = writer.Write(txt); if (ckbLogo.Checked) { using (Graphics g = Graphics.FromImage(map)) { Image logo = Image.FromFile("logo.jpg"); Graphics temp = Graphics.FromImage(logo); var w = 30; var x = (map.Width - w) / 2; var y = (map.Height - w) / 2; var rect = new Rectangle(x, y, w, w); g.FillRectangle(Brushes.Red, rect); g.DrawImage(logo, x + 2, y + 2, w - 4, w - 4); } } pictureBox1.BackColor = Color.White; pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; pictureBox1.Image = map; }
/// <summary> /// 将字符串转换为二维码图片 /// </summary> /// <param name="Str">字符串(网址)</param> /// <returns>return : 二维码图片</returns> public static Texture2D Convert_StrToQrcodeTexture2D(string Str) { Texture2D encoded = new Texture2D(256, 256); QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions(); qrEncodeOption.CharacterSet = "UTF-8"; //设置编码格式,否则读取'中文'乱码 qrEncodeOption.Height = encoded.height; qrEncodeOption.Width = encoded.width; qrEncodeOption.Margin = 0; //设置周围空白边距 var writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = qrEncodeOption }; encoded.SetPixels32(writer.Write(Str)); encoded.Apply(); return(encoded); }
private void GenerateQrcode(string url, string name) { var options = new QrCodeEncodingOptions { DisableECI = true, CharacterSet = "UTF-8", Width = 400, Height = 400 }; var writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options }; var bitmap = writer.Write(url); if (!string.IsNullOrEmpty(name)) { var path = Path.Combine(Environment.CurrentDirectory, name)+".png"; bitmap.Save(path, ImageFormat.Png); } }
// parameters: // strType 39 / 空 / static Stream BuildQrCodeImage(string path) { Hashtable param_table = ParseParameters(path, ',', '=', "url"); string strType = (string)param_table["type"]; string strCode = (string)param_table["code"]; string strWidth = (string)param_table["width"]; string strHeight = (string)param_table["height"]; int nWidth = 200; int nHeight = 200; if (string.IsNullOrEmpty(strWidth) == false) Int32.TryParse(strWidth, out nWidth); if (string.IsNullOrEmpty(strHeight) == false) Int32.TryParse(strHeight, out nHeight); string strCharset = "ISO-8859-1"; bool bDisableECI = false; BarcodeFormat format = BarcodeFormat.QR_CODE; if (strType == "39" || strType == "code_39") { format = BarcodeFormat.CODE_39; strCode = strCode.ToUpper(); // 小写字符会无法编码 } else if (strType == "ean_13") { format = BarcodeFormat.EAN_13; strCode = strCode.ToUpper(); } EncodingOptions options = new QrCodeEncodingOptions { Height = nWidth, // 400, Width = nHeight, // 400, DisableECI = bDisableECI, ErrorCorrection = ErrorCorrectionLevel.L, CharacterSet = strCharset // "UTF-8" }; if (strType == "39" || strType == "code_39" || strType == "ean_13") options = new EncodingOptions { Width = nWidth, // 500, Height = nHeight, // 100, Margin = 10 }; var writer = new BarcodeWriter { // Format = BarcodeFormat.QR_CODE, Format = format, // Options = new EncodingOptions Options = options }; try { MemoryStream result = new MemoryStream(4096); using (var bitmap = writer.Write(strCode)) { bitmap.Save(result, System.Drawing.Imaging.ImageFormat.Png); } result.Seek(0, SeekOrigin.Begin); return result; } catch (Exception ex) { Stream result = BuildTextImage("异常: " + ex.Message, Color.FromArgb(255, Color.DarkRed)); result.Seek(0, SeekOrigin.Begin); return result; } }
/// <summary> /// 获取名片二维码图片路径 /// </summary> /// <param name="mingPianId">名片编号</param> /// <param name="erWeiMaNeiRong">二维码内容</param> /// <returns></returns> string GetMingPianErWeiMaFilepath(string mingPianId, string erWeiMaNeiRong) { string filepath = "/ufiles/weixin/mingpianerweima/" + mingPianId + ".png"; string mappath = Server.MapPath(filepath); if (!File.Exists(mappath)) { var options = new QrCodeEncodingOptions { Margin = 1, DisableECI = true, CharacterSet = "UTF-8", Width = 200, Height = 200 }; BarcodeWriter writer = null; writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; writer.Options = options; Bitmap bitmap = writer.Write(erWeiMaNeiRong); string dPath = System.IO.Path.GetDirectoryName(mappath); if (!Directory.Exists(dPath)) Directory.CreateDirectory(dPath); bitmap.Save(mappath); } return filepath; }