C# Code 128 does not print out

情到浓时终转凉″ 提交于 2019-12-14 03:34:38

问题


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Î


回答1:


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)..



来源:https://stackoverflow.com/questions/39263194/c-sharp-code-128-does-not-print-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!