1使用nuget安装QRCoder、SkiaSharp、SkiaSharp.NativeAssets.Linux.NoDependencies
public IActionResult QrCode(string content, int pixel) { content = HttpUtility.UrlDecode(content); string logoPath = Path.Combine(AppContext.BaseDirectory, "wwwroot", "images", "log.png"); using (QRCodeGenerator qrGenerator = new QRCodeGenerator()) using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q)) using (PngByteQRCode qrCode = new PngByteQRCode(qrCodeData)) { var qrCodeBytes = qrCode.GetGraphic(pixel, Color.Black, Color.White); using (MemoryStream ms = new MemoryStream(qrCodeBytes)) { SKBitmap qrCodeImage = SKBitmap.Decode(ms); using (var logoStream = System.IO.File.Open(logoPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { SKBitmap logo = SKBitmap.Decode(logoStream); int logoSize = Math.Min(qrCodeImage.Width / 3, qrCodeImage.Height / 3); // 根据二维码大小调整logo大小 int x = (qrCodeImage.Width - logoSize) / 2; int y = (qrCodeImage.Height - logoSize) / 2; // 创建一个新的画布 using (var canvas = new SKCanvas(qrCodeImage)) { // 绘制logo到二维码中心 canvas.DrawBitmap(logo, new SKRect(x, y, x + logoSize, y + logoSize)); } } // 将最终的图像编码为PNG格式并返回内存流 using (var image = SKImage.FromBitmap(qrCodeImage)) using (var data = image.Encode(SKEncodedImageFormat.Png, 100)) { return File(data.ToArray(), "image/png"); } } } }
标签:core,using,new,QRCoder,logoSize,linux,var,logo,qrCodeImage From: https://www.cnblogs.com/lkd3063601/p/18428404