1、項目添加ThoughtWorks.QRCode.dll和System.Drawing.dll的引用
編碼
2、建立二維碼公共處理類(QRCodeHandler.cs)spa
/// <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; } }
調用.net
QRCodeHandler qr = new QRCodeHandler(); string path = Path.Combine("C:\\", "Down\\"); 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); //生成