使用 Zebra 打印机打印时图像模糊且模糊

发布于 2024-08-29 02:36:25 字数 1457 浏览 5 评论 0原文

我编写了一个库,它根据一些用户输入创建位图图像。然后使用斑马打印机打印该位图。我遇到的问题是斑马打印机打印的图像上的所有内容都非常微弱和模糊,但如果我将位图打印到激光打印机,它看起来完全正常。有人以前遇到过这个问题吗?如果是的话,他们是如何解决的?我几乎尝试了所有我能想到的打印机设置。

更新了如何创建位图图像的代码。

public static Bitmap GenerateLabel<T>(T obj, XmlDocument template)
    {
        try
        {
            int width = Convert.ToInt32(template.SelectSingleNode("/LABELS/@width").Value);
            int height = Convert.ToInt32(template.SelectSingleNode("/LABELS/@height").Value);

            if (obj == null || height <= 0 || width <= 0)
                throw new ArgumentException("Nothing to print");

            Bitmap bLabel = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bLabel);

            XmlNodeList fieldList = template.SelectNodes("/LABELS/LABEL");

            foreach (XmlNode fieldDetails in fieldList)
            {
                //non important code...

                    g.DrawImage(bBarCode, field.Left, field.Top);


                using (TextBox txtbox = new TextBox())
                {
                    // more non important code...

                    Rectangle r = new Rectangle(field.Left, field.Top, field.Width, field.Height);
                    txtbox.DrawToBitmap(bLabel, r);
                }
            }

            return bLabel;
        }
        catch (Exception ex)
        {
            throw new Exception("Unable to create bitmap: " + ex.Message);
        }
    }

I wrote a library which creates a bitmap image from some user input. This bitmap is then printed using a zebra printer. The problem I am running into is everything is very faint and blurry on the image printed by the zebra printer but if I print the bitmap to a laser printer it looks perfectly normal. Has anyone run into this before and if so how did they fix it? I have tried nearly everything I can think of printer settings wise.

Updated with code for how I create the bitmap images.

public static Bitmap GenerateLabel<T>(T obj, XmlDocument template)
    {
        try
        {
            int width = Convert.ToInt32(template.SelectSingleNode("/LABELS/@width").Value);
            int height = Convert.ToInt32(template.SelectSingleNode("/LABELS/@height").Value);

            if (obj == null || height <= 0 || width <= 0)
                throw new ArgumentException("Nothing to print");

            Bitmap bLabel = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bLabel);

            XmlNodeList fieldList = template.SelectNodes("/LABELS/LABEL");

            foreach (XmlNode fieldDetails in fieldList)
            {
                //non important code...

                    g.DrawImage(bBarCode, field.Left, field.Top);


                using (TextBox txtbox = new TextBox())
                {
                    // more non important code...

                    Rectangle r = new Rectangle(field.Left, field.Top, field.Width, field.Height);
                    txtbox.DrawToBitmap(bLabel, r);
                }
            }

            return bLabel;
        }
        catch (Exception ex)
        {
            throw new Exception("Unable to create bitmap: " + ex.Message);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

今天小雨转甜 2024-09-05 02:36:25

Zebra 打印驱动程序正在抖动您的输出。要为 Zebra 打印创建完美的图像,您需要以 203 DPI 和 2 色黑白(1 位)创建图像。

The Zebra print driver is dithering your output. To create a perfect image for Zebra printing, you'll need to create an image at 203 DPI and 2-color black and white (1-bit).

你是年少的欢喜 2024-09-05 02:36:25

这是所有斑马打印机的通用“功能”,驱动程序在传输到打印机本身之前使用有损技术压缩图像,据我所知,没有解决方法。

This is a universal 'feature' among all zebra printers, the drivers compress the images using a lossy technique before transmission to the printer itself, there is no workaround as far as I can tell.

无妨# 2024-09-05 02:36:25

我最终使用了一个名为 Thermal SDK 的第三方库,它允许我绘制/保存我的位图,然后以所需的“特殊”格式将其发送到斑马打印机。它适用于单个标签,但如果您想一次执行多个标签,效率会非常低,因为您必须先将每个标签保存到文件中,然后才能打印。

I ended up using a third party library called Thermal SDK which allowed me to draw/save my bitmap and then send it to the zebra printer in the 'special' format it needed. It works for single labels but if you wanted to do many at a time it would be pretty inefficient since you have to save each label to a file before you can print it.

送你一个梦 2024-09-05 02:36:25

打印机需要 1 bpp 单色图像。并且没有完美的算法将彩色或灰度图像转换为单色。根据图像的不同,此类算法可能会或可能不会产生良好的结果。因此,最好的方法是从一开始就将图像准备为单色,就像上面提到的 Mike Ransom 一样。但是,如果必须以编程方式完成,则初始彩色图像应仅使用黑白颜色(以便转换产生良好的结果),然后您可以使用这样的算法(此处的源链接):

public static Bitmap BitmapTo1Bpp(Bitmap img)
   {
       int w = img.Width;
       int h = img.Height;

       Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
       BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);

       for (int y = 0; y < h; y++)
       {
           byte[] scan = new byte[(w + 7) / 8];

           for (int x = 0; x < w; x++)
           {
               Color c = img.GetPixel(x, y);
               if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
           }

           Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);
       }

       bmp.UnlockBits(data);

       return bmp;
   }

The printer requires a 1 bpp monochrome image. And there is no perfect algorithm for converting a color or grayscale image to monochrome. Such algorithms may or may not produce a good result depending on the image. So the best way is to prepare an image to be monochrome from the beginning, like Mike Ransom stated above. But if it has to be done programmatically, the initial color image should use black and white colors only (so that the conversion produces a good result) and then you can use an algorithm like this (source link here):

public static Bitmap BitmapTo1Bpp(Bitmap img)
   {
       int w = img.Width;
       int h = img.Height;

       Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
       BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);

       for (int y = 0; y < h; y++)
       {
           byte[] scan = new byte[(w + 7) / 8];

           for (int x = 0; x < w; x++)
           {
               Color c = img.GetPixel(x, y);
               if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
           }

           Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);
       }

       bmp.UnlockBits(data);

       return bmp;
   }
雨后咖啡店 2024-09-05 02:36:25

要查看的地方是驱动程序设置,打印机的 dpi 是多少,有许多设置可能会导致效果,而不仅仅是有损技术。

我们已经向 zebras 和 intermec Thermals 发送了许多位图图像,它应该可以工作

once place to look at is the driver settings, what is the dpi on the printer, there are many settings that can be causing the effect not just the lossy technique.

we've sent many bitmap images to zebras and intermec thermals it should work

感情旳空白 2024-09-05 02:36:25

答案很简单。 Zebra 打印机仅打印黑白图像,因此在将图像发送到打印机之前,请将其转换为黑白图像。

我不是 C# 编码员,但 VB 代码看起来很相似,所以我希望他能有所帮助:

    ''' <summary>
''' Converts an image to Black and White
''' </summary>
''' <param name="image">Image to convert</param>
''' <param name="Mode">Convertion mode</param>
''' <param name="tolerance">Tolerancia del colores</param>
''' <returns>Converts an image to Black an white</returns>
''' <remarks></remarks>
Public Function PureBW(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
    Dim x As Integer
    Dim y As Integer
    If tolerance > 1 Or tolerance < -1 Then
        Throw New ArgumentOutOfRangeException
        Exit Function
    End If
    For x = 0 To image.Width - 1 Step 1
        For y = 0 To image.Height - 1 Step 1
            Dim clr As Color = image.GetPixel(x, y)
            If Mode = BWMode.By_RGB_Value Then
                If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > 383 - (tolerance * 383) Then
                    image.SetPixel(x, y, Color.White)
                Else
                    image.SetPixel(x, y, Color.Black)
                End If
            Else
                If clr.GetBrightness > 0.5 - (tolerance / 2) Then
                    image.SetPixel(x, y, Color.White)
                Else
                    image.SetPixel(x, y, Color.Black)
                End If
            End If
        Next
    Next
    Return image
End Function

Answer is easy. Zebra printers only prints Black and White, so before you send the image to the printer, convert it to black and white.

I'm not a C# coder but VB code looks similar so I hope his helps:

    ''' <summary>
''' Converts an image to Black and White
''' </summary>
''' <param name="image">Image to convert</param>
''' <param name="Mode">Convertion mode</param>
''' <param name="tolerance">Tolerancia del colores</param>
''' <returns>Converts an image to Black an white</returns>
''' <remarks></remarks>
Public Function PureBW(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
    Dim x As Integer
    Dim y As Integer
    If tolerance > 1 Or tolerance < -1 Then
        Throw New ArgumentOutOfRangeException
        Exit Function
    End If
    For x = 0 To image.Width - 1 Step 1
        For y = 0 To image.Height - 1 Step 1
            Dim clr As Color = image.GetPixel(x, y)
            If Mode = BWMode.By_RGB_Value Then
                If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > 383 - (tolerance * 383) Then
                    image.SetPixel(x, y, Color.White)
                Else
                    image.SetPixel(x, y, Color.Black)
                End If
            Else
                If clr.GetBrightness > 0.5 - (tolerance / 2) Then
                    image.SetPixel(x, y, Color.White)
                Else
                    image.SetPixel(x, y, Color.Black)
                End If
            End If
        Next
    Next
    Return image
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文