Add image to QR code generated by ZXing.Net C # MVC

0

QrCode being generated  

MyQrCodecreationclause:

publicstaticclassQRHelper{publicstaticIHtmlStringGenerateQrCode(thisHtmlHelperhtml,stringurl,stringalt="QR code", int height = 500, int width = 500, int margin = 0)
    {
        var qrWriter = new BarcodeWriter();
        qrWriter.Format = BarcodeFormat.QR_CODE;
        qrWriter.Options = new EncodingOptions() { Height = height, Width = width, Margin = margin };

        using (var q = qrWriter.Write(url))
        {
            using (var ms = new MemoryStream())
            {
                q.Save(ms, ImageFormat.Png);
                var img = new TagBuilder("img");
                img.Attributes.Add("src", String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())));
                img.Attributes.Add("alt", alt);
                return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
            }
        }
    }
}

What I wanted to do and a custom QrCode, with an image inside, follows the example image

HereistheFULLController:

usingSystem;usingSystem.Drawing.Imaging;usingSystem.IO;usingSystem.Web;usingSystem.Web.Mvc;usingZXing;usingZXing.Common;namespaceQRCodegenerator.Controllers{publicclassHomeController:Controller{//GET:HomepublicActionResultIndex(){returnView();}publicActionResultGenerate(){returnView();}}publicstaticclassQRHelper{publicstaticIHtmlStringGenerateQrCode(thisHtmlHelperhtml,stringurl,stringalt="QR code", int height = 500, int width = 500, int margin = 0)
        {
            var qrWriter = new BarcodeWriter();
            qrWriter.Format = BarcodeFormat.QR_CODE;
            qrWriter.Options = new EncodingOptions() { Height = height, Width = width, Margin = margin };

            using (var q = qrWriter.Write(url))
            {
                using (var ms = new MemoryStream())
                {
                    q.Save(ms, ImageFormat.Png);
                    var img = new TagBuilder("img");
                    img.Attributes.Add("src", String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())));
                    img.Attributes.Add("alt", alt);
                    return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
                }
            }
        }
    }
}

Here is the view:

@using QRCodegenerator.Controllers
@model QRCodegenerator.Models.QRCodeModel


@{
    ViewBag.Title = "Generate";
}

<h2>Generate QRCode</h2>


@Html.GenerateQrCode("Codigo Gerado")
    
asked by anonymous 23.04.2018 / 20:49

0 answers