//在NuGet包管理器安装ThoughtWorks.QBCode
//后台
// GET: Home
public ActionResult Index()
{
QRCodeEncoder endocder = new QRCodeEncoder();
//二维码背景颜色
endocder.QRCodeBackgroundColor = System.Drawing.Color.White;
//二维码编码方式
endocder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//每个小方格的宽度
endocder.QRCodeScale = 10;
//二维码版本号
endocder.QRCodeVersion = 5;
//纠错等级
endocder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
var person = new { Id = 1, Name = "鬈儑鬻", Gender = 1, Age = 24 };
//将json川做成二维码
Bitmap bitmap = endocder.Encode(new JavaScriptSerializer().Serialize(person), System.Text.Encoding.UTF8);
string strSaveDir = Request.MapPath("/img/");
if (!Directory.Exists(strSaveDir))
{
Directory.CreateDirectory(strSaveDir);
}
string strSavePath = Path.Combine(strSaveDir, person.Name + ".png");
if (!System.IO.File.Exists(strSavePath))
{
bitmap.Save(strSavePath);
}
ViewBag.img = "/img/" + person.Name + ".png";
return View();
}
视图代码
<div>
<img src="@ViewBag.img" alt="" />
</div>
效果图