?
Solved

Monochrome Bitmap Printing to Zebra ZM400 using ZPL II

Posted on 2013-12-05
2
Medium Priority
?
2,717 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 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
Starting up a Project
Suggested Courses

765 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