使用Zxing.net实现asp.net mvc二维码功能

时间:2021-01-13 16:21:55

新建一个html辅助类

 public static class HtmlHelperExtensions
{
public static IHtmlString GenerateRelayQrCode(this HtmlHelper html,string qrValue,int height = , int width = , int margin = )
{
var barcodeWriter = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new EncodingOptions
{
Height = height,
Width = width,
Margin = margin
}
}; using (var bitmap = barcodeWriter.Write(qrValue))
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Gif); var img = new TagBuilder("img");
img.MergeAttribute("alt", "your alt tag");
img.Attributes.Add("src", String.Format("data:image/gif;base64,{0}",
Convert.ToBase64String(stream.ToArray()))); return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
}
} }

asp.net mvc view中调用

@Html.GenerateRelayQrCode("a")