注:使用此工具类时需要引入zxing.dll工具。
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using ZXing.Common;
using ZXing;
using ZXing.QrCode;
using System.Collections;
using ZXing.QrCode.Internal;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace www.xinduofen.com
{
/// <summary>
/// 二维码和条形码的生成与解析
/// </summary>
class MyZxingTool
{
/// <summary>
/// 生成二维码并保存到内存中(UTF-8编码)
/// </summary>
/// <param name="contents">二维码字符串内容</param>
/// <param name="width">二维码宽度</param>
/// <param name="height">二维码高度</param>
/// <returns>返回为null代表生成失败</returns>
public static Bitmap encodeQRCode(String contents, int width, int height)
{
Bitmap bitmap = null;
try {
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",// 指定编码格式
ErrorCorrection = ErrorCorrectionLevel.M,// 指定纠错等级
Width = width,
Height = height
};
BarcodeWriter writer = new BarcodeWriter();
writer.Options = options;
writer.Format = BarcodeFormat.QR_CODE;//二维码
bitmap = writer.Write(contents);//二维码生成成功