[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Monochrome Bitmap Printing to Zebra ZM400 using ZPL II

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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?
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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 …
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…

834 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