一个二维码绘制与解析的小工具
本人第一次写博客,博客就是为了开源共享,顺带记录自己写代码的一些问题与小技巧。
不多说直接上图先看看效果:
具体实现代码如下:
界面就不搭了,自己动手拖一拖就好了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal;
namespace QRCode
{
public partial class FrmQR : Form
{
public FrmQR()
{
InitializeComponent();
}
private string logoPath = "";
private Bitmap curQRCode = null;
private void btn_Start_Click(object sender, EventArgs e)
{
string qRInfo = txt_QRInfo.Text.Trim();
if (qRInfo == "")
{
string errorInfo = "输入内容空,不能生成二维码!";
MessageBox.Show(errorInfo, "this");
//Bitmap bmp = new Bitmap(200, 200);
//Graphics g = Graphics.FromImage(bmp);
//Point point = new Point(260,51);
//g.DrawString(errorInfo, new Font("微软雅黑", 15, FontStyle.Bold), new SolidBrush(Color.Black), point);
//this.picBox_QR.Image = bmp;
return;
}
GenByZXingNet(qRInfo);
}
private void GenByZXingNet(string qRInfo)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
writer.Options.Height = 200;
writer.Options.Width = 200;
writer.Options.Margin = 1;
BitMatrix bm = writer.Encode(qRInfo);
Bitmap img = writer.Write(bm);
if(logoPath!="")
{
Graphics g = Graphics.FromImage(img);
Bitmap bitmapLogo = new Bitmap(logoPath);
int logoSize = 30;
bitmapLogo = new Bitmap(bitmapLogo, new System.Drawing.Size(logoSize, logoSize));
PointF point = new PointF(img.Width / 2 - logoSize / 2, img.Height / 2 - logoSize / 2);
g.DrawImage(bitmapLogo, point);
}
curQRCode = img;
this.picBox_QR.Image = img;
}
private void FrmQR_Load(object sender, EventArgs e)
{
GenByZXingNet("欢迎使用HWQ二维码生成器!");
}
/// <summary>
/// 打开一个图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Read_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图片文件(*.jpeg)|*.jpeg|图片文件(*.jpg)|*.jpg|图片文件(*.png)|*.png";
ofd.InitialDirectory = Application.ExecutablePath;
ofd.Title = "打开文件";
ofd.Multiselect = false;
ofd.ShowDialog();
logoPath = ofd.FileName;
if(logoPath=="")
{
return;
}
// 读取图片并显示
//byte[] buf = new byte[1024 * 1024 * 3]; // 图片最大3M
//using(FileStream fsRead = new FileStream(logoPath,FileMode.Open,FileAccess.Read))
//{
// int r = fsRead.Read(buf, 0, buf.Length);
// string str = Encoding.Default.GetString(buf, 0, r);
// buf = Encoding.Default.GetBytes(str);
//}
//MemoryStream ms = new MemoryStream(buf);
Size size = new System.Drawing.Size(98, 98);
Image img=Image.FromFile(logoPath);
Bitmap bmp = new Bitmap(img,size);
picBox_Logo.Image = bmp;
}
/// <summary>
/// 双击解析二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void picBox_QR_DoubleClick(object sender, EventArgs e)
{
if(curQRCode==null)
{
MessageBox.Show("当前没有二维码可解析", "提示");
return;
}
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
Result result = reader.Decode(curQRCode);
lab_Text.Text = result.Text;
}
private void btn_Save_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "图片文件(*.jpeg)|*.jpeg|图片文件(*.jpg)|*.jpg|图片文件(*.png)|*.png";
sfd.InitialDirectory = Application.ExecutablePath;
sfd.Title = "打开文件";
sfd.ShowDialog();
if(sfd.FileName=="")
{
return;
}
SaveQRCode(sfd.FileName);
}
/// <summary>
/// 保存二维码,并为二维码添加白色背景。
/// </summary>
/// <param name="path"></param>
public void SaveQRCode(string path)
{
if (curQRCode!= null)
{
Bitmap bitmap = new Bitmap(curQRCode.Width + 30, curQRCode.Height + 30);
Graphics g = Graphics.FromImage(bitmap);
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
g.DrawImage(curQRCode, new PointF(15, 15));
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png);
bitmap.Dispose();
}
}
private void picBox_QR_Click(object sender, EventArgs e)
{
}
}
}
希望以后能有更多的好东西可以和大家分享。