Code128 Barcode Generation

Source: Internet
Author: User

//
// =================================================
Barcode Printing
//
Design: Chen Tin and 2011.03
//==================================================
//
BARCODE128 has three different versions: A (number, capital letter, control character) B (number, size letter, character) C (double digit)
This procedure only handles B, c,barcode128 by the wharf, the code body, the code inspection, the code tail composition,
//
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Drawing;


Namespace Cyh. General
{
<summary>
Generate Barcode Printing class
</summary>
public class BarCode128
{
ASCII from 32 to 127 of the corresponding bar code area, consisting of 3 bars, 3 empty, a total of 11 units, symbol contains check code
Private string[] code128encoding = new string[] {
"11011001100", "11001101100", "11001100110", "10010011000", "10010001100", "10001001100", "10011001000", "10011000100 "," 10001100100 "," 11001001000 ",
"11001000100", "11000100100", "10110011100", "10011011100", "10011001110", "10111001100", "10011101100", "10011100110 "," 11001110010 "," 11001011100 ",
"11001001110", "11011100100", "11001110100", "11101101110", "11101001100", "11100101100", "11100100110", "11101100100 "," 11100110100 "," 11100110010 ",
"11011011000", "11011000110", "11000110110", "10100011000", "10001011000", "10001000110", "10110001000", "10001101000 "," 10001100010 "," 11010001000 ",
"11000101000", "11000100010", "10110111000", "10110001110", "10001101110", "10111011000", "10111000110", "10001110110 "," 11101110110 "," 11010001110 ",
"11000101110", "11011101000", "11011100010", "11011101110", "11101011000", "11101000110", "11100010110", "11101101000 "," 11101100010 "," 11100011010 ",
"11101111010", "11001000010", "11110001010", "10100110000", "10100001100", "10010110000", "10010000110", "10000101100 "," 10000100110 "," 10110010000 ",
"10110000100", "10011010000", "10011000010", "10000110100", "10000110010", "11000010010", "11001010000", "11110111010 "," 11000010100 "," 10001111010 ",
"10100111100", "10010111100", "10010011110", "10111100100", "10011110100", "10011110010", "11110100100", "11110010100 "," 11110010010 "," 11011011110 ",
"11011110110", "11110110110", "10101111000", "10100011110", "10001011110", "10111101000", "10111100010", "11110101000 "," 11110100010 "," 10111011110 ",
"10111101110", "11101011110", "11110101110", "11010000100", "11010010000", "11010011100"
};
Private Const string code128stop = "11000111010", code128end = "11"; Fixed code tail
Private enum Code128changemodes {Codea = Codeb = +, CodeC = 99}; Change
Private enum Code128startmodes {codeunset = 0, Codea =, Codeb =, CodeC = 105};//All types of terminals encoded

<summary>
Draw Code128 code (in pixels)
</summary>
public int Encodebarcode (string code, System.Drawing.Graphics g, int x, int y, int width, int height, bool showtext)
{
if (string. IsNullOrEmpty (code)) New Exception ("barcode cannot be null");
list<int> encoded = codetoencoded (code); 1. Split-Escape
Encoded.            ADD (CheckDigitCode128 (encoded)); 2. Add check Code
String encodestring = encodestring (encoded); 3. Code

if (showtext)//calculates the size of the text, the font occupies 1/4 of the image
{
Font font = new System.Drawing.Font ("XXFarEastFont-Arial", height/5f, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel, ((byte) (0)) );
SizeF size = g.measurestring (code, font);
Height = height-(int) size. Height;
g.DrawString (code, font, System.Drawing.Brushes.Black, x, y + height);
int w = Drawbarcode (g, encodestring, X, y, width, height); 4. Draw
return (int) size. Width > W? (int) size. WIDTH:W);
}
Else
Return Drawbarcode (g, encodestring, X, y, width, height); 4. Draw
}

1. Detect and split the string and join the dock
Private list<int> codetoencoded (string code)
{
list<int> encoded = new list<int> ();
int type = 0;//2:b class, 3:c class
for (int i = 0; code. Length > 0; i++)
{
int k = isnumber (code);
if (k >= 4)//consecutive even number can be preferred to use the C class (in fact, not necessarily to the C class, but can use the C class when the bar code will be shorter)
{
if (type = = 0) encoded.   ADD ((int) code128startmodes.codec); Join the Pier
else if (type!= 3) encoded. ADD ((int) (CODE128CHANGEMODES.CODEC)); Escape
Type = 3;
for (int j = 0; J < K; j = j + 2)//two digits combined for one size body
{
Encoded. ADD (Int32.Parse (code). Substring (0, 2));
Code = code. Substring (2);
}
}
Else
{
if ((int) code[0] < 32 | | (int) code[0] > 126) throw New Exception ("String must be a number or letter");
if (type = = 0) encoded.   ADD ((int) code128startmodes.codeb); Join the Pier
else if (type!= 2) encoded. ADD ((int) (CODE128CHANGEMODES.CODEB)); Escape
Type = 2;
Encoded. ADD ((int) code[0]-32);//String to ASCII-32
Code = code. Substring (1);
}
}
return encoded;
}
2. Check code
private int CheckDigitCode128 (list<int> encoded)
{
int check = encoded[0];
for (int i = 1; i < encoded. Count; i++)
Check = check + (encoded[i] * i);
Return (check% 103);
}

       //2. Encoding (corresponding to code128encoding array)
         private String encodestring (list<int> encoded)
        {
            String encodedstring = "";
            for (int i = 0; i < encoded. Count; i++)
            {
                 encodedstring + + code128encoding[encoded[i]];
           }
            encodedstring + = code128stop + code128end;// Add end code
            return encodedstring;
        }

4. Draw the barcode (return the actual image width)
private int Drawbarcode (System.Drawing.Graphics g, string encodestring, int x, int y, int width, int height)
{
int w = width/encodestring.length;
for (int i = 0; i < encodestring.length; i++)
{
G.fillrectangle (encodestring[i] = = ' 0 '? System.Drawing.Brushes.White:System.Drawing.Brushes.Black, X, y, W, height);
x = = W;
}
Return w * (encodestring.length+2);
}
Detects whether consecutive pairs of digits, returning the length of consecutive digits
private int Isnumber (string code)
{
int k = 0;
for (int i = 0; i < code. Length; i++)
{
if (char. Isnumber (Code[i])
k++;
Else
Break
}
if (k% 2!= 0) k--;
return k;
}

       ///<summary>
       / Draw Code128 code to picture
       ///</summary>
         Public Image Encodebarcode (string code, int width, int height, bool showtext)
&NBSP;&NBSP;&NBSP;&N bsp;    {
            Bitmap image = new Bitmap (width, height);
            using (Graphics g = graphics.fromimage (image )
            {
                 g.clear (Color.White);
                int w = Encodebarcode (Code, g, 0, 0, width, height, showtext);

Bitmap image2 = new Bitmap (w, height); Cut the extra blank;
using (Graphics g2 = Graphics.fromimage (Image2))
{
G2. DrawImage (image, 0, 0);
return image2;
}

}

}
<summary>
Draw Code128 code to stream
</summary>
Public byte[] Encodebarcodebyte (string code, int width, int height, bool showtext)
{
Image image = Encodebarcode (code, width, height, showtext);
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);
Byte[] Byteimage = Ms. ToArray ();
Ms. Close ();
Image. Dispose ();
return byteimage;

}
}
}

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.