C#二維碼生成,這里使用開源的ThoughtWorks.QRCode.dll庫。
1、下載ThoughtWorks.QRCode.dll庫文件,並引用到項目中。
2、創建QRCodeHandler.cs二維碼處理類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
namespace MyStudy.Utility
{
/// <summary>
/// 二維碼處理類
/// </summary>
public class QRCodeHandler
{
/// <summary>
/// 創建二維碼
/// </summary>
/// <param name="QRString">二維碼字符串</param>
/// <param name="QRCodeEncodeMode">二維碼編碼(Byte、AlphaNumeric、Numeric)</param>
/// <param name="QRCodeScale">二維碼尺寸(Version為0時,1:26x26,每加1寬和高各加25</param>
/// <param name="QRCodeVersion">二維碼密集度0-40</param>
/// <param name="QRCodeErrorCorrect">二維碼糾錯能力(L:7% M:15% Q:25% H:30%)</param>
/// <param name="filePath">保存路徑</param>
/// <param name="hasLogo">是否有logo(logo尺寸50x50,QRCodeScale>=5,QRCodeErrorCorrect為H級)</param>
/// <param name="logoFilePath">logo路徑</param>
/// <returns></returns>
public bool CreateQRCode(string QRString, string QRCodeEncodeMode, short QRCodeScale, int QRCodeVersion, string QRCodeErrorCorrect, string filePath, bool hasLogo, string logoFilePath)
{
bool result = true;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
switch (QRCodeEncodeMode)
{
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;
default:
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
break;
}
qrCodeEncoder.QRCodeScale = QRCodeScale;
qrCodeEncoder.QRCodeVersion = QRCodeVersion;
switch (QRCodeErrorCorrect)
{
case "L":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
break;
case "M":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
break;
case "Q":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
break;
case "H":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
break;
default:
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
break;
}
try
{
Image image = qrCodeEncoder.Encode(QRString, System.Text.Encoding.UTF8);
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
if (hasLogo)
{
Image copyImage = System.Drawing.Image.FromFile(logoFilePath);
Graphics g = Graphics.FromImage(image);
int x = image.Width / 2 - copyImage.Width / 2;
int y = image.Height / 2 - copyImage.Height / 2;
g.DrawImage(copyImage, new Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
image.Save(filePath);
copyImage.Dispose();
}
image.Dispose();
}
catch (Exception ex)
{
result = false;
}
return result;
}
}
}
3、調用方法,生成二維碼
QRCodeHandler qr = new QRCodeHandler(); string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"/QRCode/"; //文件目錄 string qrString = "http://blog.csdn.net/pan_junbiao"; //二維碼字符串 string logoFilePath = path + "myLogo.jpg"; //Logo路徑50*50 string filePath = path + "myCode.jpg"; //二維碼文件名 qr.CreateQRCode(qrString, "Byte", 5, 0, "H", filePath, true, logoFilePath); //生成
