C# 代码 128 不打印 [英] C# Code 128 does not print out

查看:64
本文介绍了C# 代码 128 不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初问的问题是

字体声明为

 Font font1DText = new Font("Code 128", 12);

strUserID 的内容是 ÌMachine8Î

解决方案

查看相当详尽的

请注意,它相当简单并且依赖于字符all 位于相同的给定代码集中;如果不是这种情况,您将不得不扩展它以测试字符并在另一个代码集(可能是代码 A)中的字符之前插入一个合适的移位代码.

The original question i had ask was in C# Saving a form to image without anti aliasing which i am trying to print out Code128 to a printer. As screen capture degrades the quality of the print, i had decided to write the code directly for printing instead. All images and text could be printed out properly except Code128

Below are my codes for printing Code128. The content had already been generated properly.

        SizeF sizeLoginID128 = e.Graphics.MeasureString(this.strUserID128, font1DText);
        e.Graphics.DrawString(this.strUserID128, font1DText, Brushes.Black, fInfoStartX + TEXT_LEFT_MARGIN_OFFSET, fInfoStartY + TEXT_TOP_MARGIN_OFFSET);

The font can be found in FONTS directory and it is true type font. In GUI design stage, the font is also displayed properly. However when it is print out, the code128 is missing in the print.

Does anyone has any idea why or am i not doing things correctly again

EDIT

As per request here is a screen shot of the font in the FONT folder

The declaration of the font is as

 Font font1DText = new Font("Code 128", 12);

The content of strUserID is ÌMachine8Î

解决方案

Looking at the rather exhaustive Wikipedia article on Code 128 you can see that there are really three sets of characters in it, called Code A, B and C.

128A (Code Set A) – ASCII 00-95 (0–9, A–Z and control codes), special characters, and FNC 1–4

128B (Code Set B) – ASCII 32-127 (0–9, A–Z, a–z), special characters, and FNC 1–4

128C (Code Set C) – 00–99 (encodes two digits with a single code point) and FNC1

You want to encode a mixed-case string so you want to use Code B.

Using the regular Start Code B signal : Î (0xCC ascii 204) as a prefix and the regular Stop Code Ó (0xCE ascii 206 ) as postfix:

char StartCodeB = (char)204;
char StopCode   = (char)206;

You can build the encoded string as:

string stringToPrintAsBarCode = StartCodeB + "King Kong" + checkSum +  StopCode;

Btw: I found some bar code fonts to work better than others, e.g. when it comes to printing spaces.

Also note that in addition to the Start&Stop codes a code128 encoded string must also include a checksum character.. If you leave out any or all of these three characters the printed barcode cannot be scanned!

In your example the '8' is the checksum character.

Here is a routine to calculate the checksum for a given text and a given start code:

static char checkSum128(string data, int startcode)
{
    int sum = startcode;
    for (int i = 0; i < data.Length; i++)
    {
        sum += (i + 1) * ((byte)(data[i]) - 32);
    }
    return (char)((sum % 103)+32);
}

And here is a working routine that will encode a given text with a given start code character:

string EncodeToCode128(string text, char CodeABC)
{
    char Stop = (char)206;
    char StartCode = CodeABC == 'A' ? (char)203 :
            CodeABC == 'C' ? (char)205 : (char)204; 

    char check = checkSum128(text, (byte)StartCode - 100);

    return StartCode + text  + (char)check + Stop;

}

Use it like this:

label_barCodeFont.Text = EncodeToCode128("Hello 2017", 'B');

This is the result:

Note that it is rather simple and relies on the characters all to be in the same given code set; if that is not the case, you will have to expand it to test the characters and insert a suitable shift code before those that are in another code set (probably code A)..

这篇关于C# 代码 128 不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆