using Church.Net.Utility; using Microsoft.AspNetCore.Mvc; using QRCoder; using SixLabors.Fonts; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.Processing; using System.IO; using System.Threading.Tasks; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace Church.Net.WebAPI.Controllers { [Route("api/[controller]")] [ApiController] public class QRCodeController : ControllerBase { // GET api//5 [HttpGet] public async Task Get(string content,int size) { QRCodeGenerator gen = new QRCodeGenerator(); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); var qrCodeImage = qrCode.GetGraphic(size); //string qrCodeImagePath = "/App_Data/ScaneMeQrCode.png"; //var backgroundBitmap = SixLabors.ImageSharp.Image.Load(qrCodeImagePath); //string qrCodeImagePath = Environment.GetEnvironmentVariable("AppData"); //HttpContext.Current.Server.MapPath("~/App_Data/"); //var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/ScaneMeQrCode.png"); //System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/yourXmlFile.xml"); //int positionLeft = 0; //int positionTop = 0; //var best = logic.GetById(id); //if (best != null) //{ // using (var memoryStream = new MemoryStream()) // { // //fileStream.CopyTo(memoryStream); // var image = Superimpose(best.Name, backgroundBitmap, qrCodeImage, 10, 32); // image.Scalling(75); // image.SaveAsPng(memoryStream); // byte[] byteImage = memoryStream.ToArray(); // return File(byteImage, "image/png"); // } //} using (var memoryStream = new MemoryStream()) { qrCodeImage.SaveAsJpeg(memoryStream); return File(memoryStream.ToArray(), "image/jpeg"); } } private Font arialFont; [NonAction] public Image Superimpose(string bestName, Image largeBmp, Image smallBmp, int? x = null, int? y = null) { FontCollection collection = new(); FontFamily family = collection.Add("/App_Data/arial.ttf"); arialFont = family.CreateFont(12, FontStyle.Italic); //Graphics g = Graphics.FromImage(largeBmp); //g.CompositingMode = CompositingMode.SourceOver; //smallBmp.MakeTransparent(); int margin = 5; if (!x.HasValue) { x = largeBmp.Width - smallBmp.Width - margin; } if (!y.HasValue) { y = largeBmp.Height - smallBmp.Height - margin; } var scale = 0.8; var scaleWidth = (int)(smallBmp.Width * scale); var scaleHeight = (int)(smallBmp.Height * scale); largeBmp.Mutate(x => x.DrawText(bestName, arialFont, Color.Black, new PointF(10, 10))); smallBmp.Scalling(80); largeBmp.Mutate(ctx => ctx.DrawImage(smallBmp, new Point(x.Value, y.Value), 1f)); return largeBmp; } } }