/// <summary> /// Applies the watermark to the image. /// </summary> /// <param name="imageToAddWatermarkTo"> /// The image to add the watermark to. /// </param> /// <param name="url"></param> private static void ApplyWatermark(Image <Rgba32> imageToAddWatermarkTo, string url, IImageSettings imageSettings) { Contract.Requires(imageToAddWatermarkTo != null); const int spacer = 5; string watermarkFilename = imageSettings.WatermarkImage; if (string.IsNullOrEmpty(watermarkFilename)) { return; } if (!File.Exists(watermarkFilename)) { return; } using (Image <Rgba32> watermark = SixLabors.ImageSharp.Image.Load(watermarkFilename) .CloneAs <Rgba32>()) { watermark.Mutate(operation: pc => pc.BackgroundColor(Color.Transparent)); int width = watermark.Width; int height = watermark.Height; using (Image <Rgba32> qr = QrCode.EncodeUrl(url)) { if (qr != null) { qr.Mutate(operation: px => px.BackgroundColor(Color.Transparent)); int qrWidth = qr.Width; int qrHeight = qr.Height; int qrXPos = imageToAddWatermarkTo.Width - (qrWidth + spacer); int qrYPos = imageToAddWatermarkTo.Height - (qrHeight + spacer); width = watermark.Width + (qrWidth > 0 ? qrWidth + 2 * spacer : 0); int maxHeight = Math.Max(val1: watermark.Height, qrHeight + spacer); if (imageToAddWatermarkTo.Width <= width || imageToAddWatermarkTo.Height <= maxHeight) { return; } imageToAddWatermarkTo.Mutate(operation: pc => { pc.DrawImage(image: qr, new Point(x: qrXPos, y: qrYPos), colorBlending: PixelColorBlendingMode.Overlay, alphaComposition: PixelAlphaCompositionMode.SrcOver, opacity: 1); }); } } if (imageToAddWatermarkTo.Width <= width || imageToAddWatermarkTo.Height <= height) { return; } int x = imageToAddWatermarkTo.Width - width; int y = imageToAddWatermarkTo.Height - height; imageToAddWatermarkTo.Mutate(operation: pc => { pc.DrawImage(image: watermark, new Point(x: x, y: y), colorBlending: PixelColorBlendingMode.Overlay, alphaComposition: PixelAlphaCompositionMode.SrcOver, opacity: 1); }); } }