<%@ Page Language="C#" AutoEventWireup="true" CodeFile="genQr.aspx.cs" Inherits="genQr" %> <script> var qr = "|010754500001200\r2320339993\r01\r1025"; // ตาม format genQr(qr); //เรียกใช้ function genQr(); </script> <body>Tag HTML <div class="genQrPng"> <div> นี้ไปว่างตรงส่วนที่จะให้ QR Code แสดง
</body>
function genQr(genQr) { var data_post = JSON.stringify({ 'genQr': genQr }); alert(genQr) $.ajax({ type: "POST", url: "genQr.aspx/genQrcode", data: data_post, cache: false, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { img = '<img style="width: 110px;height: 110px;position: relative;top: -10px; left: -10px;" src="' + data.d + '">'; img2 = '<img style="width: 200px;height: 200px;position: relative;" src="' + data.d + '"">'; $('.genQrPng').html(img) $('.genQrPng2').html(img2) }, error: function (data) { //console.log } }); }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Drawing; using System.Web.Services; using System.Web.Script.Services; using System.Web.Script.Serialization; using System.Collections.Specialized; using QRCoder; public partial class genQr : System.Web.UI.Page { [WebMethod] public static string genQrcode(string genQr) { string Empty = genQr; string code = genQr; QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q); System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WaebControls.Image(); imgBarCode.Height = 95; imgBarCode.Width = 95; using (Bitmap bitMap = qrCode.GetGraphic(20)) { using (MemoryStream ms = new MemoryStream()) { bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] byteImage = ms.ToArray(); imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage); } return imgBarCode.ImageUrl; } } }