1 dll文件引用
nuget中搜索ZXing.Net安装
using ZXing;
using ZXing.QrCode.Internal;
using ZXing.Common;
2 生成一维码
注意:只能生成数字或者字母数据的一维码
代码如下:
public static Bitmap 1DBarCode(string text)
{
BarcodeWriter writer = new BarcodeWriter();
//ITF 格式不能被现在常用的支付宝、微信扫出来
//想生成可识别的可以使用 CODE_128 格式
//writer.Format = BarcodeFormat.ITF;
writer.Format = BarcodeFormat.CODE_128;
EncodingOptions options = new EncodingOptions()
{
//控制生成的一维码大小
Width = 150,
Height = 50,
Margin = 2,
//true:一维码下方不显示数字或者字母false:显示,默认是false
PureBarcode = true
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
3 二维码
3.1 不带logo
代码如下:
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="str">要生成二维码的字符串</param>
/// <param name="width">二维码图片宽度</param>
/// <param name="height">二维码图片高度</param>
/// <returns></returns>
private Bitmap 2DBarCode(String str, Int32 width, Int32 height)
{
Bitmap result = null;
try
{
BarcodeWriter barCodeWriter = new BarcodeWriter();
barCodeWriter.Format = BarcodeFormat.QR_CODE;
barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
barCodeWriter.Options.Height = height;
barCodeWriter.Options.Width = width;
barCodeWriter.Options.Margin = 0;
ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
result = barCodeWriter.Write(bm);
}
catch (Exception ex)
{
//异常输出
}
return result;
}
3.2 带logo
/// <summary>
/// 生成带Logo的二维码
/// </summary>
/// <param name="text">要生成二维码的字符串</param>
/// <param name="logo">二维码中的logo图像</param>
/// <returns></returns>
static Bitmap 3DBarCodeWithLogo(string text, Bitmap logo)
{
Bitmap bitmap = null;
//Logo 图片
//Bitmap logo = new Bitmap(logo);
//构造二维码写码器
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//生成二维码
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
//获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle();
//计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
int middleL = (map.Width - middleW) / 2;
int middleT = (map.Height - middleH) / 2;
//将img转换成bmp格式,否则后面无法创建Graphics对象
Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmpimg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(map, 0, 0);
}
//将二维码插入图片
Graphics myGraphic = Graphics.FromImage(bmpimg);
//白底
myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
bitmap = bmpimg;
return bitmap;
}
4 解析一维码和二维码
代码如下
/// <summary>
/// 解码二维码
/// </summary>
/// <param name="barcodeBitmap">待解码的一/二维码图片</param>
/// <returns>扫码结果</returns>
private string DecodeQrCode(Bitmap barcodeBitmap)
{
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
var result = reader.Decode(barcodeBitmap);
return (result == null) ? null : result.Text;
}
5 最后再来几个和图片有关的小方法
5.1 载入图片与保存图片
载入代码:
string filePath = "";
//选择文件
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选择图片";
dialog.Filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jpg;*.png;*.jpeg;*.bmp;*.gif|All files(*.*)|*.*";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filePath = dialog.FileName;
if (File.Exists(filePath))
{
byte[] data = File.ReadAllBytes(filePath);
using (MemoryStream ms = new MemoryStream(data))
{
pictureBox1.Image = new Bitmap(Image.FromStream(ms));
}
}
}
保存代码
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = "保存图片";
dialog.Filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jpg|*.png|*.jpeg|*.bmp|*.gif|All files(*.*)|*.*";
dialog.FilterIndex = 1;
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(dialog.FileName);
}
else
{
//MessageBox.Show("取消保存");
return;
}
5.2 图片与Base64互相转换
图片转base64代码:
public string ImgToBase64String(Image bmp)
{
try
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return Convert.ToBase64String(arr);
}
catch (Exception ex)
{
return null;
}
}
Base64转图片代码:
public Bitmap Base64StringToImage(string strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap img = (Bitmap)Image.FromStream(ms);
return img;
}
catch (Exception ex)
{
return null;
}
}