From cbdfdd0010ccf51df9218cd3e6b77b78849f49da Mon Sep 17 00:00:00 2001 From: Jeff Johnson Date: Fri, 18 Nov 2022 16:12:36 -0700 Subject: [PATCH] Optimization for printing images In some cases you have to chunk through images that are very tall (> 2048 pixels height). This allows to print from the raw bit pixel buffers of those images rather than having to save them to png and then pass the png bytes to the print image command. --- .../BaseCommandEmitter/ImageCommands.cs | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/ESCPOS_NET/Emitters/BaseCommandEmitter/ImageCommands.cs b/ESCPOS_NET/Emitters/BaseCommandEmitter/ImageCommands.cs index e711b3a..5affc60 100644 --- a/ESCPOS_NET/Emitters/BaseCommandEmitter/ImageCommands.cs +++ b/ESCPOS_NET/Emitters/BaseCommandEmitter/ImageCommands.cs @@ -3,6 +3,10 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; +using System.Drawing; + +using static System.Net.Mime.MediaTypeNames; + namespace ESCPOS_NET.Emitters { public abstract partial class BaseCommandEmitter : ICommandEmitter @@ -40,6 +44,22 @@ public virtual byte[] SetImageDensity(bool isHiDPI) } public virtual byte[] BufferImage(byte[] image, int maxWidth = -1, bool isLegacy = false, int color = 1) + { + int width; + int height; + byte[] imageData; + using (var img = SixLabors.ImageSharp.Image.Load(image)) + { + imageData = img.ToSingleBitPixelByteArray(maxWidth: maxWidth == -1 ? (int?)null : maxWidth); + height = img.Height; + width = img.Width; + } + + return BufferImageSingleBitPixelByteArray(imageData, width, height, isLegacy, color); + } + + public virtual byte[] BufferImageSingleBitPixelByteArray(byte[] singleBitPixelByteArray, + int width, int height, bool isLegacy = false, int color = 1) { ByteArrayBuilder imageCommand = new ByteArrayBuilder(); @@ -57,16 +77,6 @@ public virtual byte[] BufferImage(byte[] image, int maxWidth = -1, bool isLegacy break; } - int width; - int height; - byte[] imageData; - using (var img = Image.Load(image)) - { - imageData = img.ToSingleBitPixelByteArray(maxWidth: maxWidth == -1 ? (int?)null : maxWidth); - height = img.Height; - width = img.Width; - } - byte heightL = (byte)height; byte heightH = (byte)(height >> 8); @@ -84,7 +94,7 @@ public virtual byte[] BufferImage(byte[] image, int maxWidth = -1, bool isLegacy imageCommand.Append(new byte[] { 0x30, 0x70, 0x30, 0x01, 0x01, colorByte, widthL, widthH, heightL, heightH }); } - imageCommand.Append(imageData); + imageCommand.Append(singleBitPixelByteArray); // Load image to print buffer ByteArrayBuilder response = new ByteArrayBuilder(); @@ -119,5 +129,17 @@ public virtual byte[] PrintImage(byte[] image, bool isHiDPI, bool isLegacy = fal return ByteSplicer.Combine(SetImageDensity(isHiDPI), BufferImage(image, maxWidth, isLegacy, color), WriteImageFromBuffer()); } } + + public virtual byte[] PrintImageSingleBitPixelByteArray(byte[] singleBitPixelByteArray, int width, int height, bool isHiDPI, bool isLegacy = false, int maxWidth = -1, int color = 1) + { + if (isLegacy) + { + return ByteSplicer.Combine(BufferImageSingleBitPixelByteArray(singleBitPixelByteArray, width, height, isLegacy)); + } + else + { + return ByteSplicer.Combine(SetImageDensity(isHiDPI), BufferImageSingleBitPixelByteArray(singleBitPixelByteArray, width, height, isLegacy, color), WriteImageFromBuffer()); + } + } } }