using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtWorks.QRCode.Codec.Util; using System.IO; namespace QRCode { public partial class FMain : Form { public FMain() { InitializeComponent(); string[] cbversion = new string[41]; for (int i = 0; i <= 40; i++) { cbversion[i] = i.ToString(); } cbVersion.DataSource = cbversion; cbVersion.Text = "7"; cbEncoding.Text = "Byte"; cbCorrectionLevel.Text = "M"; } private void btnEncode_Click(object sender, EventArgs e) { string encoding = cbEncoding.Text; string correctionLever = cbCorrectionLevel.Text; int version = Convert.ToInt32(cbVersion.Text); int scale; if (!int.TryParse(txtScale.Text.Trim(), out scale)) { MessageBox.Show("Scale必须为数字"); return; } string data = txtData.Text.Trim(); if (data == string.Empty) { MessageBox.Show("数据不能为空,请输入数据哦!"); return; } QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();//创建一个对象 switch (encoding)//设置编码模式 { case "Byte": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; break; case "AlphaNumeric": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; break; case "Numeric": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC; break; } //设置编码测量度 qrCodeEncoder.QRCodeScale = scale; //设置编码版本 qrCodeEncoder.QRCodeVersion = version; if (correctionLever == "L")//设置编码错误纠正 { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; } else if (correctionLever == "M") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; } else if (correctionLever == "Q") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q; } else if (correctionLever == "H") { qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; } qrCodeEncoder.QRCodeForegroundColor = btnQRCodeForegroundColor.BackColor;//设置二维码前景色 qrCodeEncoder.QRCodeBackgroundColor = btnQRCodeBackgroundColor.BackColor;//设置二维码背景色 Image image = qrCodeEncoder.Encode(data,Encoding.UTF8);//生成二维码图片 if (txtLogo.Text.Trim() != string.Empty)//如果有logo的话则添加logo { Bitmap btm = new Bitmap(txtLogo.Text); Bitmap copyImage = new Bitmap(btm,image.Width/5,image.Height/5); Graphics g = Graphics.FromImage(image); int x = image.Width / 2 - copyImage.Width / 2; int y = image.Height / 2 - copyImage.Height / 2; g.DrawImage(copyImage,x,y); } picEncode.Image = image; } private void btnDecode_Click(object sender, EventArgs e) { try { string decodedString = new QRCodeDecoder().decode(new QRCodeBitmapImage(new Bitmap(picEncode.Image)),Encoding.UTF8); txtData.Text = decodedString; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnSave_Click(object sender, EventArgs e)//保存二维码到磁盘 { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png"; sfd.Title = "保存二维码"; sfd.FileName = string.Empty; if (picEncode.Image != null) { if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName != "") { using (FileStream fs = (FileStream)sfd.OpenFile()) { switch (sfd.FilterIndex) { case 1: picEncode.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: picEncode.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp); break; case 3: picEncode.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Gif); break; case 4: picEncode.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Png); break; } } MessageBox.Show("恭喜,保存成功!"); } } else { MessageBox.Show("抱歉,没有要保存的图片哦!"); } } private void btnPrint_Click(object sender, EventArgs e) { } private void btnOpen_Click(object sender, EventArgs e)//打开要选的logo文件 { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png"; if (ofd.ShowDialog() == DialogResult.OK) { String fileName = ofd.FileName; picEncode.Image = new Bitmap(fileName); } } private void btnChooseLogo_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png"; if (ofd.ShowDialog() == DialogResult.OK) { txtLogo.Text = ofd.FileName; } } private void btnQRCodeForegroundColor_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); if (cd.ShowDialog() == DialogResult.OK) { btnQRCodeForegroundColor.BackColor = cd.Color; } } private void btnQRCodeBackgroundColor_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); if (cd.ShowDialog() == DialogResult.OK) { btnQRCodeBackgroundColor.BackColor = cd.Color; } } } }
原文地址:https://www.cnblogs.com/xiaowie/p/8693621.html
时间: 2024-10-09 01:18:14