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

新建一个html辅助类

 public static class HtmlHelperExtensions

    {

        public static IHtmlString GenerateRelayQrCode(this HtmlHelper html,string qrValue,int height = 250, int width = 250, int margin = 0)

        {

            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")

你可能感兴趣的:(asp.net)