Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printing Images - Height #111

Closed
Gi4nfratti opened this issue Apr 27, 2021 · 4 comments
Closed

Printing Images - Height #111

Gi4nfratti opened this issue Apr 27, 2021 · 4 comments

Comments

@Gi4nfratti
Copy link

Gi4nfratti commented Apr 27, 2021

Hello, first of all, the library is incredible!! I made a question on Stackoverflow (for more details), about the height of the image when I print. I'm using Xamarin Android, I created a bitmap with SkiaSharp and save it as PNG. It's perfectly saved on my device. But when I'll print it, the necessary height to print the whole bitmap is not matching. I tried with this image, it's a logo with a width more larger than the height, 3840x2160, and it worked perfectly. And my bitmap have the height more larger than the width (it's a list), 700 of width and 1280 of height. Both images are PNG and 72 of DPI. I'm reducing the width as follows:

buffer = ByteSplicer.Combine( e.PrintImage(logo, true, true, 372, 0) );

If I don't reduce the width, it prints a lot of random characters.
The width is perfect, but I have the height similar to the first image, it seems a "logo size".

Can you show an example of how can we print images/bitmaps that have a big height like a list without cutting/limiting it?

Tests:
I Printed this PNG file:
Nike-Logo

And this is the result:
nikeResult

But when I print this:
verticalLogo

That's the result:
verticalResult

In this test, it still shows this thing next to the sticker, but with another image that I tested, it printed the height of the sticker (same cutted as the test above) and just it.

@lukevp
Copy link
Owner

lukevp commented Sep 5, 2021

Hi, sorry for the delay getting back to you. What happens if you set the width on the image and print it? Are there other too-tall images that print correctly? The reason I ask is that the bar prints the whole way down, so that makes me think the problem might be with the PNG you are using and not with the lib resizing the image.

@Gi4nfratti
Copy link
Author

Hello, no problem. This was resolved by dividing the bitmap in parts of 256px of height and then printing.

@jjxtra
Copy link

jjxtra commented Nov 18, 2022

Had to do something similar, sharing for future generations...

    private static IEnumerable<Image<Rgba32>> ExtractChunks(Image<Rgba32> sourceImage)
    {
        const int chunkSize = 256;
        for (var y = 0; y < sourceImage.Height; y += chunkSize)
        {
            Rectangle sourceArea = new(0, y, sourceImage.Width, Math.Min(sourceImage.Height - y, chunkSize));
            if (sourceArea.Height < 1)
            {
                yield break;
            }
            Image<Rgba32> targetImage = new(sourceArea.Width, sourceArea.Height);
            int height = sourceArea.Height;
            sourceImage.ProcessPixelRows(targetImage, (sourceAccessor, targetAccessor) =>
            {
                for (int i = 0; i < height; i++)
                {
                    Span<Rgba32> sourceRow = sourceAccessor.GetRowSpan(sourceArea.Y + i);
                    Span<Rgba32> targetRow = targetAccessor.GetRowSpan(i);

                    sourceRow.Slice(sourceArea.X, sourceArea.Width).CopyTo(targetRow);
                }
            });
            yield return targetImage;
        }
    }

@nivle
Copy link

nivle commented Jun 13, 2023

I ended up doing solving this like this, to solve some buffer issues, and some slow image printing issues.
I found out that if the image width is above a certain resolution(for my printer > 173(which seems to be 512px))
the printer will print the image very slow, kind of like a legacy printer, it will print the chunks created below one by one with a delay. Once I set the image resolution to 173(went -1 starting from 180(this is my printer dpi)), the image got printed much faster, not sure why this is the case. Setting the maxwidth to 512px in the PrintImage function also fixes this issue.


IList<byte[]> data = new List<byte[]>
            {
                epson.CenterAlign()
            };

Image image = Image.FromStream("///");//add here your image

int size = 256;

for (int i = 0; i < Math.Round((double)image.Height / size, 0, MidpointRounding.ToPositiveInfinity); i++)
{
    int width = image.Width;
    int height = size * (i + 1) <= image.Height ? size : image.Height - (size * i);

    Bitmap newImage = new Bitmap(width, height);

    using (Graphics graphics = Graphics.FromImage(newImage))
    {
        graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, i * size, width, height), GraphicsUnit.Pixel);
    }

    using (MemoryStream memoryStream = new MemoryStream())
    {
        newImage.Save(memoryStream, ImageFormat.Tiff);
        data.Add(epson.PrintImage(memoryStream.ToArray(), false, false, 512));
    }
}

data.Add(epson.FullCutAfterFeed(0));

return ByteSplicer.Combine(data.ToArray());


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants