-1

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 folderenter image description here

The declaration of the font is as

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

The content of strUserID is ÌMachine8Î

Community
  • 1
  • 1
ishtarsg
  • 133
  • 10
  • __Code128__ is the name of a barcode font. If that is not what you are talking about, please correct the question and tag! Also tell us which font you __are__ using and show is the declaration of the variable `strUserID128`! – TaW Sep 01 '16 at 06:59
  • 1
    Hi TaW i had updated the question. Please have a look. Do advice which tag is wrong too – ishtarsg Sep 01 '16 at 07:30
  • Ah, so it __is__ a bar code you are trying to print? In that case the Tag is correct! [I have seen the issue (or soemthing quite similar )before](http://stackoverflow.com/questions/35229541/barcode-128-space/35243321?s=1|3.5530#35243321) and then a change of font was the best solution.. The `Î` character may be the problem; bar code fonts are really just for digits, capital letter and a few special codes. – TaW Sep 01 '16 at 07:57
  • Hi I believe the ÌMachine8Î is correct as i am able to read from the handheld scanner if i print via notepad. Removing Î character makes it unreadable – ishtarsg Sep 01 '16 at 08:05
  • Hm, `Î` is the Function 4 (FNC4), said to be 'not widely supported'. You actually want a [Start Code B](https://en.wikipedia.org/wiki/Code_128) signal, meaning the rest can be 32-127 and a few extras. Can you try `Ñ` (0xCE ascii 206) as prefix and maybe a Stop Code `Ó` (0xD3 ascii 211 ) as postfix? – TaW Sep 01 '16 at 08:26
  • Hi Taw. Thanks for the reply again.if i use the prefix and stop code, i am actually getting 2 empty box on both end of the code. The funny thing is now the original 128 font is working after i download another font and installed! – ishtarsg Sep 01 '16 at 08:34
  • I'm afraid I got a typo in there, make it Ñ (0xD1 ascii 209) – TaW Sep 01 '16 at 08:46
  • Did you use the corrected code? – TaW Sep 01 '16 at 08:55
  • Hi Taw. Yes the encoding on FNC4 is correct. Now the font is able to show and decode properly. I believe the first time i did things, the code128 font may not be registered properly – ishtarsg Sep 01 '16 at 09:12
  • Well is did show in the font info. I have three different code128 fonts installed here.. – TaW Sep 01 '16 at 09:34

1 Answers1

2

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:

enter image description here

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

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • I have updated and expanded the answer to include a simple string encoding functiob. It is tested for a few simple Code Set B strings.. I have also deleted the remarks about you using wrong startcodes. I must have misread the characters you posted, or maybe you have corrected them ?-) – TaW Sep 02 '16 at 17:49