using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; using Church.Net.Entity; using Church.Net.Utility; using Microsoft.AspNetCore.Mvc; using QRCoder; using SixLabors.Fonts; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Processing; using WebAPI.Logics; using WebAPI.Logics.Interface; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace WebAPI.Controllers { [Route("[controller]/[action]")] [ApiController] public class BestController : ApiControllerBase { private readonly ICrudLogic logic; private readonly ICrudLogic weekLogic; private readonly PastoralDomainLogic cellGroupLogic; public BestController( ICrudLogic logic, ICrudLogic weekLogic, PastoralDomainLogic cellGroupLogic ) : base(logic) { this.logic = logic; this.weekLogic = weekLogic; this.cellGroupLogic = cellGroupLogic; } // GET api//5 public override Task> GetAll() { return base.GetAll(); } public override Task GetById(string id) { return Task.Run(() => { var best = logic.GetById(id); best.HappinessGroup = cellGroupLogic.GetById(best.GroupId); best.HappinessGroup.HappinessWeeks = weekLogic.GetAll(w => w.GroupId == best.GroupId).OrderBy(w=>w.SEQ).ToList(); return best; }); } [HttpGet()] public async Task GetInvitationQRcode(string id) { QRCodeGenerator gen = new QRCodeGenerator(); QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode($"https://happiness.tours/invitation/{id}", QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); var qrCodeImage = qrCode.GetGraphic(3); 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"); } } return this.NotFound(); } [HttpPost] public int UpdateBestWeek(HappinessWeek week) { return weekLogic.CreateOrUpdate(week,out string id); } 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; } } }