1、创建MVC项目
2、添加NuGet程序包:ThoughtWorks.QRCode
3、在控制器书写对应代码
[HttpPost]
public ActionResult EWMTest(string txtQRContext)
{
string strContent = txtQRContext;
MemoryStream ms = new MemoryStream();
// 生成二维码
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 7;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//kedee 增加utf-8编码,可支持中文汉字
Image image = qrCodeEncoder.Encode(strContent, System.Text.Encoding.UTF8);
Bitmap map = new Bitmap(image, new Size(200, 200));
//end
map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
// 将图片内存流转成base64,图片以DataURI形式显示
string strUrl = "data:image/gif;base64," + Convert.ToBase64String(ms.ToArray());
image.Dispose();
ms.Dispose();
// 显示
ViewBag.src = strUrl;
//前端显示:<img src="@ViewBag.src" />
return View();
}
本文转载自:https://blog.csdn.net/YiQiJinBu/article/details/23673727?utm_source=blogxgwz2
标签:Asp,string,image,QRCodeEncoder,MVC,ms,new,Net,qrCodeEncoder From: https://www.cnblogs.com/hcj123/p/17421673.html