[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Monochrome Bitmap Printing to Zebra ZM400 using ZPL II

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

650 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