Solved

Monochrome Bitmap Printing to Zebra ZM400 using ZPL II

Posted on 2013-12-05
2
2,426 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
2 Comments
 
LVL 25

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now