From e4051a3dacca35289e9f8993fb258648f8f9a9c5 Mon Sep 17 00:00:00 2001 From: Reporting Issue <> Date: Thu, 14 Sep 2023 22:43:42 -0400 Subject: [PATCH] add opaque option to drawer properties + add background code --- .../ElementDrawers/DrawerOptions.cs | 5 +++++ src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/BinaryKits.Zpl.Viewer/ElementDrawers/DrawerOptions.cs b/src/BinaryKits.Zpl.Viewer/ElementDrawers/DrawerOptions.cs index d87b2e63..38c0bbf3 100644 --- a/src/BinaryKits.Zpl.Viewer/ElementDrawers/DrawerOptions.cs +++ b/src/BinaryKits.Zpl.Viewer/ElementDrawers/DrawerOptions.cs @@ -10,6 +10,11 @@ public class DrawerOptions public SKEncodedImageFormat RenderFormat { get; set; } = SKEncodedImageFormat.Png; + /// + /// Applies label over a white background after rendering all elements + /// + public bool OpaqueBackground { get; set; } = false; + public int RenderQuality { get; set; } = 80; public bool ReplaceDashWithEnDash { get; set; } = true; diff --git a/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs b/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs index b8d11d0a..7291f6c2 100644 --- a/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs +++ b/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs @@ -100,7 +100,26 @@ public byte[] Draw( } } - using var data = skBitmap.Encode(_drawerOptions.RenderFormat, _drawerOptions.RenderQuality); + SKBitmap finalBitmap; + if (this._drawerOptions.OpaqueBackground == true) + { + finalBitmap = new SKBitmap(labelImageWidth, labelImageHeight); + using (SKCanvas canvas = new SKCanvas(finalBitmap)) + { + SKPaint paint = new SKPaint + { + IsAntialias = true, + FilterQuality = SKFilterQuality.High + }; + canvas.Clear(SKColors.White); + canvas.DrawBitmap(skBitmap, 0, 0, paint); + } + } + else { + finalBitmap = skBitmap; + } + + using var data = finalBitmap.Encode(_drawerOptions.RenderFormat, _drawerOptions.RenderQuality); return data.ToArray(); }