devs121
devs121

Reputation: 59

Print Image ESC/POS

I'm using this API to print images in my thermal printer with Bluetooth connection. I tested using this image that have 3840x2160 of resolution, and it prints perfectly. I drew a bitmap using SkiaSharp, save it on my device, but I'm trying to print it and I can't set the height.

I tried some resolutions:

1280x720

720x720

Even if my image have 3840 of width (e.g the example of the link), the function put the limit of 372, and it works, but not with height.

I can't control the height and it's cutting my bitmap and just printing not even the half of it.

My bitmap is a list, probably 372 of width and 800 of height.

Code:

            EPSON e = new EPSON();
            ConnectBluetooth("ThermalPrinter");
            var outStream = (OutputStreamInvoker)socket.OutputStream;
            string directory = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
            string logoPath = System.IO.Path.Combine(directory, "bmp.png");
            var logo = System.IO.File.ReadAllBytes(logoPath);

            byte[] buffer = null;

            buffer = ByteSplicer.Combine(
                e.PrintImage(logo, true, true, 372, 0)
            );
            await outStream.WriteAsync(buffer, 0, buffer.Length);
            buffer = null;

            socket.Close();
            socket.Dispose();

On the PrintImage it goes to:

byte[] PrintImage(byte[] image, bool isHiDPI, bool isLegacy = false, int maxWidth = -1, int color = 1)

and I receive this byte[] and send to print. Link here.

PS: I already change the parameters of PrintImage but it still printing a "logo size" image, not the whole image. If I reduce the font size of the text inside my bitmap, I see more text when i print it, so it could be the height of what it will print, not a problem on the bitmap (when I open it in the gallery of my device, is perfect).

Upvotes: 0

Views: 2220

Answers (1)

Emilio Cano
Emilio Cano

Reputation: 11

If you need resize you image, try to use this

    public Image EscalarImagen(Image imagen, int maxAncho, int maxAlto)
    {
        var ratioX = (double)maxAncho / imagen.Width;
        var ratioY = (double)maxAlto / imagen.Height;
        var ratio = Math.Min(ratioX, ratioY);

        var newWidth = (int)(imagen.Width * ratio);
        var newHeight = (int)(imagen.Height * ratio);

        var newImage = new Bitmap(newWidth, newHeight);
        Graphics.FromImage(newImage).DrawImage(imagen, 0, 0, newWidth, newHeight);
        return newImage;
    }

Upvotes: 1

Related Questions