【问题标题】:How to generate a dynamic GRF image to ZPL ZEBRA print如何为 ZPL ZEBRA 打印生成动态 GRF 图像
【发布时间】:2012-12-12 20:10:00
【问题描述】:

我有问题。

我正在生成动态 BMP 图像并尝试通过 ZPL 命令将其发送到 ZEBRA 打印机。 我需要将我的 BMP 转换为 GRF 图像。我认为我的 BMP 图像提取的十六进制不正确。

打印的图像模糊且不正确。

这是我的代码:

string bitmapFilePath = @oldArquivo;  // file is attached to this support article
byte[] bitmapFileData = System.IO.File.ReadAllBytes(bitmapFilePath);
int fileSize = bitmapFileData.Length;

Bitmap ImgTemp = new Bitmap(bitmapFilePath);
Size ImgSize = ImgTemp.Size;
ImgTemp.Dispose();

// The following is known about test.bmp.  It is up to the developer
// to determine this information for bitmaps besides the given test.bmp.            
int width = ImgSize.Width;
int height = ImgSize.Height;
int bitmapDataOffset = 62; // 62 = header of the image
int bitmapDataLength = fileSize - 62;// 8160;    

double widthInBytes = Math.Ceiling(width / 8.0);    

// Copy over the actual bitmap data from the bitmap file.
// This represents the bitmap data without the header information.
byte[] bitmap = new byte[bitmapDataLength];
Buffer.BlockCopy(bitmapFileData, bitmapDataOffset, bitmap, 0, (bitmapDataLength));

// Invert bitmap colors
for (int i = 0; i < bitmapDataLength; i++)
{
    bitmap[i] ^= 0xFF;
}

// Create ASCII ZPL string of hexadecimal bitmap data
string ZPLImageDataString = BitConverter.ToString(bitmap).Replace("-", string.Empty);

string comandoCompleto = "~DG" + nomeImagem + ".GRF,0" + bitmapDataLength.ToString() + ",0" + widthInBytes.ToString() + "," + ZPLImageDataString;

【问题讨论】:

  • 图片的bitmapDataOffset头在这里是什么意思?如何获得 62 的值?

标签: c# image printing bmp zpl


【解决方案1】:

试试下面的代码。 未测试!

public static string CreateGRF(string filename, string imagename)
{
    Bitmap bmp = null;
    BitmapData imgData = null;
    byte[] pixels;
    int x, y, width;
    StringBuilder sb;
    IntPtr ptr;

    try
    {
        bmp = new Bitmap(filename);
        imgData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed);
        width = (bmp.Width + 7) / 8;
        pixels = new byte[width];
        sb = new StringBuilder(width * bmp.Height * 2);
        ptr = imgData.Scan0;
        for (y = 0; y < bmp.Height; y++)
        {
            Marshal.Copy(ptr, pixels, 0, width);
            for (x = 0; x < width; x++)
                sb.AppendFormat("{0:X2}", (byte)~pixels[x]);
            ptr = (IntPtr)(ptr.ToInt64() + imgData.Stride);
        }
    }
    finally
    {
        if (bmp != null)
        {
            if (imgData != null) bmp.UnlockBits(imgData);
            bmp.Dispose();
        }
    }
    return String.Format("~DG{0}.GRF,{1},{2},", imagename, width * y, width) + sb.ToString();
}

【讨论】:

  • 嗨!谢谢您的回复。这对我来说非常重要。此行有错误:“sb.AppendFormat("{0:X2}", (byte)~pixels[x]);"这是错误:“算术运算导致溢出。”你能帮助我吗?谢谢!
  • 嘿!我认为通过您的功能和一些比特挖掘,我完成了我的问题,呵呵。感谢您的回答 Jigsore
  • @Jigsore 请你告诉我你的答案..我应用了相同的答案..但它在imagename中显示错误请告诉我
  • 你的代码很棒,但对我来说有一个小错误。在我的转换中,它的右侧总是有黑边。我不知道为什么,我通过将每行中的最后一个像素设置为0来解决它。
  • 帮助为什么我的图像的所有行都显示为 FFFFFFFFFFF,基于 png 文件路径。图像大小重要吗?我有一个 14kb png 文件 728x529 尺寸单色。
【解决方案2】:

需要指出的一点是,要转换的位图必须是单色的(即每像素 1 位)。 Zebra 的知识库中有一个示例演示了在 ZPL 中打印简单的单色图像:https://km.zebra.com/kb/index?page=answeropen&type=open&searchid=1356730396931&answerid=16777216&iqaction=5&url=https%3A%2F%2Fkm.zebra.com%2Fkb%2Findex%3Fpage%3Dcontent%26id%3DSA304%26actp%3Dsearch%26viewlocale%3Den_US&highlightinfo=4194550,131,153#。如果您可以将图像转换为单色位图,那么您应该能够遵循该示例。

// Given a monochrome bitmap file, one can read
// information about that bitmap from the header
// information in the file.  This information includes
// bitmap height, width, bitsPerPixel, etc.  It is required
// that a developer understands the basic bitmap format and
// how to extract the following data in order to proceed.
// A simple search online for 'bitmap format' should yield
// all the needed information.  Here, for our example, we simply
// declare what the bitmap information is, since we are working
// with a known sample file.

string bitmapFilePath = @"test.bmp";  // file is attached to this support article
byte[] bitmapFileData = System.IO.File.ReadAllBytes(bitmapFilePath);
int fileSize = bitmapFileData.Length;

// The following is known about test.bmp.  It is up to the developer
// to determine this information for bitmaps besides the given test.bmp.
int bitmapDataOffset = 62;
int width = 255;
int height = 255;
int bitsPerPixel = 1; // Monochrome image required!
int bitmapDataLength = 8160;
double widthInBytes = Math.Ceiling(width / 8.0);

// Copy over the actual bitmap data from the bitmap file.
// This represents the bitmap data without the header information.
byte[] bitmap = new byte[bitmapDataLength];
Buffer.BlockCopy(bitmapFileData, bitmapDataOffset, bitmap, 0, bitmapDataLength);

// Invert bitmap colors
for (int i = 0; i < bitmapDataLength; i++)
{
    bitmap[i] ^= 0xFF;
}

// Create ASCII ZPL string of hexadecimal bitmap data
string ZPLImageDataString = BitConverter.ToString(bitmap);
ZPLImageDataString = ZPLImageDataString.Replace("-", string.Empty);

// Create ZPL command to print image
string[] ZPLCommand = new string[4];

ZPLCommand[0] = "^XA";
ZPLCommand[1] = "^FO20,20";
ZPLCommand[2] =
    "^GFA, " +
    bitmapDataLength.ToString() + "," +
    bitmapDataLength.ToString() + "," +
    widthInBytes.ToString() + "," +
    ZPLImageDataString;

ZPLCommand[3] = "^XZ";

// Connect to printer
string ipAddress = "10.3.14.42";
int port = 9100;
System.Net.Sockets.TcpClient client =
    new System.Net.Sockets.TcpClient();
client.Connect(ipAddress, port);
System.Net.Sockets.NetworkStream stream = client.GetStream();

// Send command strings to printer
foreach (string commandLine in ZPLCommand)
{
    stream.Write(ASCIIEncoding.ASCII.GetBytes(commandLine), 0, commandLine.Length);
    stream.Flush();
}

// Close connections
stream.Close();
client.Close();

【讨论】:

  • 您好!谢谢您的回复。我的代码基于您发布的这个示例。但我需要具有动态宽度和高度的动态图像。我在另一个函数上生成图像,它将把创建的图像带到这个函数中。然后,我需要一个具有动态参数的函数,该函数通过位图参数生成 GRF。你能帮助我吗?再次感谢您!
  • @lucasrhuan 你得到这个答案了吗?我现在正在做同样的应用程序。我需要制作一个 C# 应用程序,它将我的 .bmp 文件转换为 .GRF 文件。例如,我使用 .net paint 创建 pcx 文件,然后使用 Ztools 创建 .GRF 文件。但我真的需要制作可以将我的 .bmp 文件转换为 grf 并发送到打印的单个 C# 应用程序。发送到打印机已经在做..我只需要这个.bmp到.grf的转换..我的意思是我需要以编程方式转换任何想法!!!..如果你得到答案,请让我非常感谢..跨度>
  • @jason.zissman - 感谢您发布代码(Zebra 杀死了链接!)
【解决方案3】:

请将 2 添加到 widthInBytes - 它有效!!!

        int bitmapDataOffset = int.Parse(bitmapFileData[10].ToString()); ;
        int width = 624;// int.Parse(bitmapFileData[18].ToString()); ;
        int height = int.Parse(bitmapFileData[22].ToString()); ;
        int bitsPerPixel = int.Parse(bitmapFileData[28].ToString()); // Monochrome image required!
        int bitmapDataLength = bitmapFileData.Length - bitmapDataOffset;
        double widthInBytes = Math.Ceiling(width / 8.0)+2;

【讨论】:

  • 您能否为您的答案添加一些解释?只显示代码可能会造成混淆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多