1. First download NuGet package (ZXing.Net)
2. Create a new controller and write code behind
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using ZXing; using ZXing.QrCode; namespace WebApplication1.Controllers { public class StrController : Controller { // GET: Str public ActionResult Index() { return View(); } /// <summary> /// generating method of two-dimensional code /// </ Summary> /// <param name = "text"> input string </ param> /// <param name = "width"> two-dimensional code width < / param> /// <param name = "height"> two-dimensional code height </ param> /// <Returns> </ Returns> public String QRcode ( String text, String width, String height) { String the Response = " " ; try { BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; QrCodeEncodingOptions Options = new new QrCodeEncodingOptions (); options.DisableECI = to true ; // set the content encoding options.CharacterSet = " UTF-. 8 " ; // the width and height values assigned to the transmitted two-dimensional code = options.Width Convert.ToInt32 (width); options.Height = Convert.ToInt32 (height); // set two-dimensional code margin, the pixel unit is not fixed options.Margin = . 1 ; writer.Options = Options; Bitmap the Map = writer.Write (text); String DI = text + DateTime.Now.ToString ( " yyyyMMddHHmmss " ) + " .png " ; // two-dimensional code will be displayed on the desktop (you also want to displayed on the desktop, then change it to the path) String path Path.Combine = ( " C: \\ \\ zhulin the Users \\ Desktop " , DI); map.Save (path, ImageFormat.Png); map.Dispose () ; the Response = " two-dimensional code generating success! " ; } the catch (Exception) { the Response = " two-dimensional code generation failed! " ; } return the Response; } } }
3. The front end
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <link href="~/Scripts/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" /> <script src="~/Scripts/jquery-3.3.1.min.js"></script> <script src="~/Scripts/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#btn").click(function () { var w = $("#wd").val(); var h = $("#hg").val(); var text = $("#tx").val(); $.ajax({ url: "/Str/QRcode", data: "text=" + text + "&width=" + w + "&height=" + h, success: function (e) { alert(e); } }); }); }) </script> </head> <body> <div style="margin-top:20px;margin-left:20px;"> <p>高度:<input type="text" style="width:60px;height:32px;border:1px solid #66b1ff;margin-left:3px;" id="wd" /><span style="margin-left:10px;">宽度:</span><input type="text" style="width:60px;height:32px;border:1px solid #66b1ff;margin-left:3px;" id="hg" /></p> <input type="text" style="width:200px;height:32px;border:1px solid #66b1ff;" id="tx" placeholder="请输入字符串..." /><button type="button" class="btn btn-info" id="btn" style="margin-left:5px;margin-top:-1px;height:33px;">提交</button> </div> </body> </html>
4. Effects: