简体   繁体   English

C#代码128不打印出来

[英]C# Code 128 does not print out

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. 我曾问过的最初问题是在C#中将表单保存到图像中而没有抗锯齿 ,我试图将Code128打印到打印机。 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 除Code128外,所有图像和文本均可正确打印

Below are my codes for printing Code128. 以下是我打印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. 该字体可以在FONTS目录中找到,它是真正的字体。 In GUI design stage, the font is also displayed properly. 在GUI设计阶段,字体也会正确显示。 However when it is print out, the code128 is missing in the print. 但是,当打印出来时,打印中缺少代码128。

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 根据要求,这里是FONT文件夹中字体的屏幕截图 在此处输入图片说明

The declaration of the font is as 字体的声明为

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

The content of strUserID is ÌMachine8Î strUserID的内容是Ì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. 查看有关Code 128的详尽的Wikipedia文章,您会发现其中确实包含三组字符,分别称为Code A,B和C。

128A (Code Set A) – ASCII 00-95 (0–9, A–Z and control codes), special characters, and FNC 1–4 128A(代码集A)– ASCII 00-95(0-9,AZ和控制代码),特殊字符和FNC 1-4

128B (Code Set B) – ASCII 32-127 (0–9, A–Z, a–z), special characters, and FNC 1–4 128B(代码集B)– ASCII 32-127(0-9,AZ,AZ),特殊字符和FNC 1-4

128C (Code Set C) – 00–99 (encodes two digits with a single code point) and FNC1 128C(代码集C)– 00–99(用单个代码点编码两位数字)和FNC1

You want to encode a mixed-case string so you want to use Code B. 您要对大小写混合的字符串进行编码,因此要使用代码B。

Using the regular Start Code B signal : Î (0xCC ascii 204) as a prefix and the regular Stop Code Ó (0xCE ascii 206 ) as postfix: 使用常规的开始代码B信号:前缀Î (0xCC ascii 204)和作为后缀的常规停止代码 Ó (0xCE ascii 206):

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 , eg 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 ! 还要注意,除了Start&Stop代码外,code128编码的字符串还必须包含一个校验和字符 。如果您遗漏了这三个字符中的任何一个或全部,则无法扫描打印的条形码!

In your example the '8' is the checksum character. 在您的示例中,“ 8”是校验和字符。

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).. 如果不是这种情况,则必须将其扩展以测试字符,并在另一个代码集中的代码(可能是代码A)之前插入合适的移位代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM