Thermally sensitive print monochrome bitmap

Source: Internet
Author: User

Epson Thermal Printer through the Posdll call Pos_predownloadbmptoram, Pos_s_printbmpinram function for movie tickets, such as monochrome bitmap QR code printing, and after converting monochrome bitmap, You need to set the number of palette colors (10) and the number of important colors (11) for bitmap bitmaps in the image information header to 0, and also, bitmap data size (7) is (2).

  1. size: 4 bytes,0x28 = bytes, indicating the large length of the info header for a total of bytes
  2. width: 4 bytes,0x10 = 16, image width is pixel
  3. height: 4 bytes,0x10 = 16, image height is pixel
  4. planes: 2 bytes,0x01 = 1, the number of bit polygons is 1
  5. bits: 2 bytes,0x20 = 32, each pixel requires 32bits
  6. compression: 4 bytes,0 means no compression
  7. imagesize: 4 bytes,0x400 = 1024x768 bytes, bitmap data size is 1024x768 bytes
  8. xresolution: 4 bytes, horizontal resolution
  9. yresolution: 4 bytes, vertical resolution
  10. ncolours: 4 bytes, the number of palette colors used by the bitmap
  11. importantcolours: 4 bytes, number of important colors

The picture is quoted from "http://blog.csdn.net/o_sun_o/article/details/8351037".

How to do this:

<summary>
Generate two-dimensional code codes
</summary>
<param name= "TXT_QR" > need to generate QR code information </param>
<param name= "Txt_size" > value the larger the resulting QR code picture the higher the pixel </param>
<returns> QR code image information </returns>
private static string Generateqrcode (String txt_qr, String txt_size)
{
Generate two-dimensional code
string filepath = string. Empty;
String qrencoding = "Byte";
String level = "M";
String txt_ver = "0";

Qrcodeencoder Qrcodeencoder = new Qrcodeencoder ();
String encoding = qrencoding;
if (encoding = = "Byte")
{
Qrcodeencoder.qrcodeencodemode = Qrcodeencoder.encode_mode. BYTE;
}
else if (encoding = = "alphanumeric")
{
Qrcodeencoder.qrcodeencodemode = Qrcodeencoder.encode_mode. Alpha_numeric;
}
else if (encoding = = "Numeric")
{
Qrcodeencoder.qrcodeencodemode = Qrcodeencoder.encode_mode. NUMERIC;
}

string errorcorrect = level;
if (Errorcorrect = = "L")
Qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction. L
else if (Errorcorrect = = "M")
Qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction. M
else if (Errorcorrect = = "Q")
Qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction. Q;
else if (Errorcorrect = = "H")
Qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction. H

Try
{
int scale = convert.toint16 (txt_size);
Qrcodeencoder.qrcodescale = scale;///size (the larger the value, the higher the image pixel of the resulting QR code)
int version = Convert.ToInt16 (Txt_ver);
Qrcodeencoder.qrcodeversion = version;

String data = TXT_QR;
Bitmap image = Qrcodeencoder.encode (data);
int image_width = image. Width;
int image_height = image. Height;
Bitmap resizedbmp = new Bitmap (Image_width + 4, image_height + 4);
Graphics g = graphics.fromimage (resizedbmp);
G.clear (Color.White);//refresh with background color
Create rectangle for displaying image.
Rectangle destrect = new Rectangle (2, 2, image_width, image_height);
Create rectangle for source image.
Rectangle srcrect = new Rectangle (0, 0, image_width, image_height);
Draw image to screen.
G.drawimage (image) Image, Destrect, Srcrect, GraphicsUnit.Pixel); G.dispose ();
filepath = CONVERTTOBPP (resizedbmp);

Resizedbmp.dispose ();
Image. Dispose ();
}
catch (Exception ex)
{
MessageBox.Show (ex. Message, "information tip");
}
return filepath;
}

<summary>
Ways to convert monochrome bitmaps
</summary>
<param name= "img" ></param>
private static Bitmap XXX (Bitmap img)
{
int w = img. Width;
int h = img. Height;
Bitmap bmp = New Bitmap (W, H, pixelformat.format1bppindexed);

BitmapData data = bmp. LockBits (New Rectangle (0, 0, W, h), Imagelockmode.readwrite, pixelformat.format1bppindexed);
for (int y = 0; y < h; y++)
{
byte[] Scan = new byte[(w + 7)/8];
for (int x = 0; x < W; + +)
{
Color C = img. GetPixel (x, y);
if (c.getbrightness () >= 0.5) SCAN[X/8] |= (byte) (0x80 >> (x% 8));
}
Marshal.Copy (Scan, 0, (INTPTR) (int) data. Scan0 + data. Stride * y), scan. Length);
}
Bmp. Palette = Creategrayscalepalette ();
return BMP;
}

///Create a color palette corresponding to the image format
///</summary>
//<param name= "PixelFormat" > Image format, only format1bppindexed, Format1bppindexed,format1bppindexed</param>
//<returns> Returns the palette, or null if creation fails or image format is not supported. </returns>
private static ColorPalette Createcolorpalette (PixelFormat pixelformat)
{
ColorPalette palette = NULL;
if (PixelFormat = = Pixelformat.format1bppindexed | | pixelformat = = Pixelformat.format4bppindexed | | pixelFormat = = Pi xelformat.format8bppindexed)
{
//Because the ColorPalette class has no constructors, create a 1x1 bitmap here, and then grab the bitmap's palette
Bitmap temp = new Bitmap (1, 1, PixelFormat);
Palette = temp. Palette;
Temp. Dispose ();
}
return palette;
}

<summary>
Create a corresponding palette based on color depth
</summary>
<param name= "Depth" > color depth, which is the number of digits used to represent the color </param>
<returns> Return to Palette </returns>
private static ColorPalette Createcolorpalette (int depth)
{
Decide what color palette to use depending on the number of colors
PixelFormat PixelFormat = pixelformat.format1bppindexed;
if (Depth > 2)
PixelFormat = pixelformat.format4bppindexed;
if (Depth > 16)
PixelFormat = pixelformat.format8bppindexed;
Return Createcolorpalette (PixelFormat);
}

<summary>
Create a monochrome grayscale palette
</summary>
<returns> Return to Palette </returns>
private static ColorPalette Creategrayscalepalette ()
{
ColorPalette palette = Createcolorpalette (pixelformat.format1bppindexed);
Palette. Entries[0] = Color.FromArgb (0, 0, 0, 0);
Palette. ENTRIES[1] = Color.FromArgb (0, 255, 255, 255);
return palette;
}

The

/**
* byte array takes an int value, and this method applies to the order of (low in front, high in the rear).
*
* @param ary
* byte array
* @param offset
* Start from the offset bit of the array
* @return int value
*/
Private static int bytestoint (byte[] ary, int offset)
{
int value;
value = (int) ((Ary[offset] & 0xFF)
| ((Ary[offset + 1] << 8) & 0xff00)
| ((Ary[offset + 2] <<) & 0xFF0000)
| ((Ary[offset + 3] <<) & 0xff000000));
return value;
}


<summary>
When converted to a monochrome bitmap, the number of colors used in the bitmap in the image information header and the specified number of important colors are set to 0
</summary>
<param name= "img" ></param>
<returns></returns>
private static string converttobpp (Bitmap Bitmap)
{
Open an image of either indexed or non-indexed color
Bitmap BM = XXX (BITMAP);
MemoryStream ms = new MemoryStream ();
Bm. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);
byte[] buffer = new Byte[ms. Length];
Image.Save () changes the position of MemoryStream and requires a re-seek to begin
Ms. Seek (0, Seekorigin.begin);
Ms. Read (buffer, 0, buffer. Length);

byte[] Bitlenght = new Byte[4];
Array.copy (buffer, 2, bitlenght, 0, 4);
int ibitlen = bytestoint (bitlenght, 0);

byte[] Bitbuffer = new byte[2];
Bitbuffer[0] = 0x2C;
BITBUFFER[1] = 0x04;
Array.copy (bitbuffer, 0, buffer, 34, 2);
Array.copy (bitlenght, 0, Buffer, 34, 4);

byte[] Bitmapbuffer = new Byte[1];
Bitmapbuffer[0] = 0;
Array.copy (bitmapbuffer, 0, Buffer, 46, 1);
Array.copy (bitmapbuffer, 0, buffer, 50, 1);

FileStream fs = null;
string file = Guid.NewGuid (). ToString (). Replace ("-", ""). ToString () + ". bmp";
Converting pending data from a string into a byte array
FS = File.openwrite (File);
Set the start of writing to the end of the file
Fs. Position = 0;//fs. Length
Append the content to be written to the end of the file
Fs. Write (buffer, 0, Ibitlen);
Fs. Close ();
Bm. Dispose ();
return file;
}

Thermally sensitive print monochrome bitmap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.