1、安装ThoughtWorks.QRCode包
2、引入
using ThoughtWorks.QRCode.Codec;
3、具体代码
public string DrawQRCode(string enCodeString, string width, string height) { string downPath = string.Empty; System.Drawing.Bitmap bt; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字) qrCodeEncoder.QRCodeScale = 10;//大小(值越大生成的二维码图片像素越高) qrCodeEncoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误) qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级) qrCodeEncoder.QRCodeBackgroundColor = Color.White;//背景色 qrCodeEncoder.QRCodeForegroundColor = Color.Black;//前景色 bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8); string filename = enCodeString;// "code"; string file_path = filePath.Substring(filePath.LastIndexOf("\")); string codeUrl = filePath + "/" + filename + ".jpg"; downLoadPath = downLoadPath.Substring(downLoadPath.LastIndexOf("/")); downPath = downLoadPath + "/" + filename + ".jpg"; //根据文件名称,自动建立对应目录 if (!System.IO.Directory.Exists(file_path)) { System.IO.Directory.CreateDirectory(file_path); } bt.Save(codeUrl);//保存图片 return downPath; }