using Microsoft.AspNetCore.Mvc; using System.DrawingCore; using System.DrawingCore.Imaging; using ZXing; using ZXing.Common; using ZXing.QrCode; using ZXing.ZKWeb; namespace JCT.Web.Controllers { public class TestController : Controller { private Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv; public TestController( Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv) { this.hostingEnv = hostingEnv; } public ActionResult Index() { string text = "http://www.niunan.net"; int width = 300; int height = 300; int heig = width; if (width > height) { heig = height; width = height; } if (string.IsNullOrWhiteSpace(text)) { return null; } var w = new QRCodeWriter(); BitMatrix b = w.encode(text, BarcodeFormat.QR_CODE, width, heig); var zzb = new BarcodeWriter(); zzb.Options = new EncodingOptions() { Margin = 0, }; Bitmap b2 = zzb.Write(b); // 將Bitmap轉換爲字節數組 using (MemoryStream memoryStream = new MemoryStream()) { b2.Save(memoryStream, ImageFormat.Jpeg); byte[] imageBytes = memoryStream.ToArray(); // 設置HTTP響應的Content-Type Response.ContentType = "image/jpeg"; // 將字節數組寫入輸出流 return File(imageBytes, "image/jpeg"); } } } }