ThoughtWorks.QRCode和ZXing.Net 二维码,网址可以直接跳转
1、项目引用 ThoughtWorks.QRCode和ZXing.Net 二维码的相关生成类库,管理NuGet程序包搜索添加ThoughtWorks.QRCode和ZXing.Net包
2、添加按钮生成二维码按钮(ThoughtWorks.QRCode和ZXing.Net 两个按钮),添加picturebox显示二维码
3、ThoughtWorks.QRCode和ZXing.Net 两个按钮事件的主要代码如下
ThoughtWorks.QRCode和ZXing.Net
#region 二维码 int i = 2; /// <summary> /// 生成二维码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnProduct_Click(object sender, EventArgs e) { #region ThoughtWorks.QRCode 二维码,网址可以直接跳转 ThoughtWorks.QRCode.Codec.QRCodeEncoder endocder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder(); //二维码背景颜色 endocder.QRCodeBackgroundColor = System.Drawing.Color.White; //二维码编码方式 endocder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE; //每个小方格的宽度 endocder.QRCodeScale = 4; //二维码版本号 endocder.QRCodeVersion = 5;//控制版本,不同版本,显示出来的样式不一样 //纠错等级 endocder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M; //将json做成二维码 //序列化对象 //var person = new { Id = i, Name = "66666", Gender = 1, Age = 24 + i++, oid = i, url = "https://www.baidu.com/" }; //using (Bitmap bitmap = endocder.Encode(new JavaScriptSerializer().Serialize(person), System.Text.Encoding.UTF8)) //using (Bitmap bitmap = endocder.Encode(new JavaScriptSerializer().Serialize(person))) // 链接地址 var person = "http://www.baidu.com/"; using (Bitmap bitmap = endocder.Encode(person)) { //Bitmap bitmap = endocder.Encode(new JavaScriptSerializer().Serialize(person), System.Text.Encoding.UTF8); var strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode{i}.jpg"); //strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode.jpg"); if (!Directory.Exists(Path.GetDirectoryName(strSavePath))) { Directory.CreateDirectory(strSavePath); } //System.Runtime.InteropServices.ExternalException: //使用错误的图像格式保存图像。 - 或 - 图像已保存到同一文件从创建它。出现一般都是保存路经问题,或者aspnet中是读写文件权限问题 bitmap.Save(strSavePath, System.Drawing.Imaging.ImageFormat.Jpeg); pbxPicture.SizeMode = PictureBoxSizeMode.Zoom; //pbxQRCode.Image = Bitmap.FromHbitmap(bitmap.GetHbitmap()); //第一种绘图 IntPtr hBitmap = bitmap.GetHbitmap(); pbxQRCode.Image = Bitmap.FromHbitmap(hBitmap); //pbxQRCode.Image.Save(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); DeleteObject(hBitmap); bitmap.Dispose(); //第二种绘图 //Graphics graphics = Graphics.FromImage(bitmap); //graphics.Clear(Color.White); ////再bitmap上绘图 //graphics.DrawImage(new Bitmap(""), new PointF(0, 0)); //graphics.Dispose(); } //解密二维码 //ThoughtWorks.QRCode.Codec.QRCodeDecoder qRCodeDecoder = new ThoughtWorks.QRCode.Codec.QRCodeDecoder(); //var decoderResult = qRCodeDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(new Bitmap(strSavePath))); #endregion #region ZXing.Net 二维码,网址可以直接跳转 //var textBoxText = "http://106.12.79.39/#/login?redirect=%2Fdashboard"; //textBoxText = "https://www.baidu.com/"; //BarcodeWriter barCodeWriter = new BarcodeWriter(); //barCodeWriter.Format = BarcodeFormat.QR_CODE; //barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); //barCodeWriter.Options.Height = 230; //barCodeWriter.Options.Width = 230; //BitMatrix bm = barCodeWriter.Encode(textBoxText); //Bitmap img = barCodeWriter.Write(bm); //pbxQRCode.Image = img; //pbxQRCode.SizeMode = PictureBoxSizeMode.Zoom; #endregion } private void btnZxing_Click(object sender, EventArgs e) { #region ZXing.Net 二维码,网址可以直接跳转 ThoughtWorks.QRCode 二维码 //var textBoxText = "http://106.12.79.39/#/login?redirect=2Fdashboard&ee=6"; //textBoxText = "https://www.baidu.com?redirect=2Fdashboard&ee=6"; //var textBoxText = new { Id = i, Name = "wolfy", Gender = 1, Age = 24 + i++, oid = i, url = "https://www.baidu.com/" }; BarcodeWriter barCodeWriter = new BarcodeWriter(); barCodeWriter.Format = BarcodeFormat.QR_CODE; barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); barCodeWriter.Options.Height = 230; barCodeWriter.Options.Width = 230; //链接地址 //textBoxText = "https://www.baidu.com?redirect=2Fdashboard&ee=6"; //BitMatrix bm = barCodeWriter.Encode(textBoxText); //序列化对象 //var textBoxText = new { Id = i, Name = "wolfy", Gender = 1, Age = 24 + i++, oid = i, url = "https://www.baidu.com/" }; //BitMatrix bm = barCodeWriter.Encode(new JavaScriptSerializer().Serialize(textBoxText)); //字符串 var textBoxText = "https://www.baidu.com?redirect=2Fdashboard&ee=6"; BitMatrix bm = barCodeWriter.Encode(textBoxText); Bitmap img = barCodeWriter.Write(bm); pbxQRCode.Image = img; pbxQRCode.SizeMode = PictureBoxSizeMode.Zoom; #endregion } /// <summary> /// 解密二维码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnConsume_Click(object sender, EventArgs e) { var strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode{i}.jpg"); //Bitmap bitmap = new Bitmap(new MemoryStream(File.ReadAllBytes(strSavePath))); using (Bitmap bitmap = new Bitmap(new MemoryStream(File.ReadAllBytes(strSavePath)))) { strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode.jpg"); ThoughtWorks.QRCode.Codec.QRCodeDecoder qRCodeDecoder = new ThoughtWorks.QRCode.Codec.QRCodeDecoder(); //var decoderResult = qRCodeDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(new Bitmap(strSavePath))); var decoderResult = qRCodeDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(bitmap)); lblTakephotoSavePath.Text = $"二维码:{decoderResult}"; } } #endregion