生成QRCode后,我需要使用MVC C#在内部创建一种方法来将QRCode Webpath存储在数据库中,但是我不知道如何编码。
以下是我的控制器代码:
[HttpPost]
public ActionResult Generate(QRCodeModel qrcode)
{
try
{
qrcode.QRCodeImagePath = GenerateQRCode($"{ qrcode.QRCodeText}", $"http://sradmindashboardyumeng.azurewebsites.net/{qrcode.QRCodeText}");
ViewBag.Message = "QR Code Created successfully";
}
catch (Exception ex)
{
}
return View(qrcode);
}
public string GenerateQRCode(string qrcodeText, string qrcodeUrl)
{
string folderPath = $"~/assets/images/QRcode/";
//string webPath = $"~/assets/images/QRcode/{qrcodeText}.jpg";
string imagePath = $"~/assets/images/QRcode/{qrcodeText}.jpg";
var barcodeWriter = new BarcodeWriter();
barcodeWriter.Format = BarcodeFormat.QR_CODE;
var result = barcodeWriter.Write(qrcodeUrl);
string barcodePath = System.Web.HttpContext.Current.Server.MapPath(imagePath);
var barcodeBitmap = new Bitmap(result);
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(barcodePath, FileMode.Create, FileAccess.ReadWrite))
{
barcodeBitmap.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
return imagePath;
}
}
}