Solved

Monochrome Bitmap Printing to Zebra ZM400 using ZPL II

Posted on 2013-12-05
2
2,578 Views
Last Modified: 2013-12-08
Hello,

I am using the following code which I got from a thread online:

// 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();

Open in new window


It is working correctly, if I use a monochrome bitmap of the same size as used in this example, but I need to use a smaller monochrome bitmap image, but when I change the width & height above to a smaller image I get the following error:

Error
I have updated the width and height to the smaller image as shown below:

// 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 = 50;
        int height = 50;
        int bitsPerPixel = 1; // Monochrome image required!
        int bitmapDataLength = 8160;
        double widthInBytes = Math.Ceiling(width / 8.0);

Open in new window


...but I need to update the other variables too, I am however unsure of how to get this information of the monochrome bitmap image. Please advise how I can get the other ones from my monochrome bitmap image?

Thanks!
0
Comment
Question by:SpaceLaika
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 250 total points
ID: 39700271
You should set bitmapDataLength too, 1 bit per pixel (in one row: (width / 8) bytes):
int bitmapDataLength = ((width / 8) * height);

Open in new window


bitmapDataOffset is not always the same, it is written in header:
http://en.wikipedia.org/wiki/BMP_file_format

int bitmapDataOffset = BitConverter.ToInt32(bitmapFileData, 10);

Open in new window

0
 
LVL 16

Assisted Solution

by:DansDadUK
DansDadUK earned 250 total points
ID: 39701078
As sinisav indicates, you need to understand the (various) bitmap header formats, in order to extract the attributes of the image.

The Image Bitmap tool in the PCL Paraphernalia application (available via http://www.pclparaphernalia.eu ) converts common formats of bitmap image to the equivalent PCL raster image.

I've no idea how much PCL raster format differs from ZPL raster format, but the tool may at least give you some help, since (for supported bitmap types) it will display some of the bitmap properties:

width (pixels)
height (pixels)
X-resolution (pixels-per-metre and (derived) pixels-per-inch)
Y-resolution (pixels-per-metre and (derived) pixels-per-inch)
bits-per-pixel
compression mode number
monochrome - true/false


The bitmap image types supported  by the tool are Windows v3 bitmap files, of type BM, which are uncompressed, bottom-up, and either 1-, 4- or 24-bits-per-pixel images.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question