# C# 使用ZXing產出QRCode ``` using ZXing; using ZXing.QrCode; using System.Drawing; /// <summary> /// QRCode中間放Icon /// </summary> public static Bitmap overlay_o { get; set; } /// <summary> /// 產生QrCode上面後製添加文字 /// </summary> /// <param name="content"></param> /// <param name="height"></param> /// <param name="width"></param> /// <returns></returns> public static Bitmap QREncodeAndAddText(string content, string qrCodeText, int height = 500, int width = 500) { var writer = new BarcodeWriter //dll裡面可以看到屬性 { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions //設定大小 { Height = height, Width = width, } }; var result = writer.Write(content); overlay_o = (Bitmap)Image.FromFile(HttpContext.Current.Server.MapPath("~/images/Icon2.png")); Bitmap overlay = new Bitmap(overlay_o, new Size(50,50)); int deltaHeigth = result.Height - overlay.Height; int deltaWidth = result.Width - overlay.Width; Graphics g = Graphics.FromImage(result); g.DrawImage(overlay, new Point((deltaWidth / 2), (deltaHeigth / 2))); //控制QrCode原始圖片大小 var img = new Bitmap(result.Width, result.Height + 50); using (g = Graphics.FromImage(img)) using (var font = new Font("Microsoft JhengHei", 28))//文字字型大小 using (var brush = new SolidBrush(Color.Black)) using (var bgBrush = new SolidBrush(Color.White)) using (var format = new StringFormat() { Alignment = StringAlignment.Center }) { //填滿白色 g.FillRectangle(bgBrush, 0, 0, img.Width, img.Height); //QRCode原始圖片座標位移(留白) g.DrawImage(result, new Point(0, 25)); //填入文字、座標位移 g.DrawString(qrCodeText, font, brush, result.Width / 2, 10, format); } return img; } ```