Assume the order number of the EAN content from right to left: N13, n12, N11, N10, Beijing, n8, N7, N6, N5, N4, N3, N2, N1, among them, N1 is the verification bit required for preparation:
1. C1 = (n2 + N4 + N6 + n8 + N10 + n12) * 3;
2. C2 = N3 + N5 + N7 + 5E + N11 + N13;
3. Add C1 and C2, and set their single digits to C3. 4. Check position n1 = 10-c3
Using system;
Using system. collections;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. Windows. forms;
Namespace barcodeean13
{
/// <Summary>
/// Barcode control.
/// </Summary>
Public class wctrl_barcode
{
Private Static string getena13code (string numbers12bit)
{
Int C1 = 0;
Int C2 = 0;
For (INT I = 0; I <11; I = I + 2)
{
C1 + = int. parse (numbers12bit [I]. tostring ());
C2 + = int. parse (numbers12bit [I + 1]. tostring ());
}
Int C3 = C1 + C2 * 3;
C3 = C3-C3/10*10;
If (C3 = 0)
{
Return numbers12bit + 0;
}
Else
{
Int n = 10-C3;
Return numbers12bit + N;
}
}
/// <Summary>
/// Paint ean13 barcode to specified graphics into specified draw rectangle.
/// </Summary>
/// <Param name = "barcode"> barcode value. </param>
/// <Param name = "G"> graphics where to draw. </param>
/// <Param name = "drawbounds"> draw bounds. </param>
Public static void paint_ean13 (string numbers12bit, graphics g, rectangle drawbounds)
{
String barcode = getena13code (numbers12bit );
Char [] symbols = barcode. tochararray ();
// --- Validate barcode -------------------------------------------------------------------//
If (barcode. length! = 13 ){
Return;
}
Foreach (char C in symbols ){
If (! Char. isdigit (c )){
Return;
}
}
// --- Check barcode checksum ------------------------//
Int checksum = convert. toint32 (symbols [12]. tostring ());
Int calcsum = 0;
Bool one_three = true;
For (INT I = 0; I <12; I ++ ){
If (one_three ){
Calcsum + = (convert. toint32 (symbols [I]. tostring () * 1 );
One_three = false;
}
Else {
Calcsum + = (convert. toint32 (symbols [I]. tostring () * 3 );
One_three = true;
}
}
Char [] calcsumchar = calcsum. tostring (). tochararray ();
If (checksum! = 0 & checksum! = (10-convert. toint32 (calcsumchar [calcsumchar. Length-1]. tostring ()))){
Return;
}
//--------------------------------------------------//
// Outputs //---------------------------------------------------------------------------------------//
Font font = new font ("Microsoft sans serif", 8 );
// Fill backround with white color
// G. Clear (color. White );
Int linewidth = 1;
Int x = drawbounds. X;
// Paint human readable 1 system symbol code
G. drawstring (symbols [0]. tostring (), Font, new solidbrush (color. Black), X, drawbounds. Y + drawbounds. Height-16 );
X + = 10;
// Paint left 'Guard Bars', always same '000000'
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. White, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
// First number of barcode specifies how to encode each character in the left-hand
// Side of the barcode shocould be encoded.
Bool [] leftsideparity = new bool [6];
Switch (symbols [0])
{
Case '0 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = true; // odd
Leftsideparity [2] = true; // odd
Leftsideparity [3] = true; // odd
Leftsideparity [4] = true; // odd
Leftsideparity [5] = true; // odd
Break;
Case '1 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = true; // odd
Leftsideparity [2] = false; // even
Leftsideparity [3] = true; // odd
Leftsideparity [4] = false; // even
Leftsideparity [5] = false; // even
Break;
Case '2 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = true; // odd
Leftsideparity [2] = false; // even
Leftsideparity [3] = false; // even
Leftsideparity [4] = true; // odd
Leftsideparity [5] = false; // even
Break;
Case '3 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = true; // odd
Leftsideparity [2] = false; // even
Leftsideparity [3] = false; // even
Leftsideparity [4] = false; // even
Leftsideparity [5] = true; // odd
Break;
Case '4 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = false; // even
Leftsideparity [2] = true; // odd
Leftsideparity [3] = true; // odd
Leftsideparity [4] = false; // even
Leftsideparity [5] = false; // even
Break;
Case '5 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = false; // even
Leftsideparity [2] = false; // even
Leftsideparity [3] = true; // odd
Leftsideparity [4] = true; // odd
Leftsideparity [5] = false; // even
Break;
Case '6 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = false; // even
Leftsideparity [2] = false; // even
Leftsideparity [3] = false; // even
Leftsideparity [4] = true; // odd
Leftsideparity [5] = true; // odd
Break;
Case '7 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = false; // even
Leftsideparity [2] = true; // odd
Leftsideparity [3] = false; // even
Leftsideparity [4] = true; // odd
Leftsideparity [5] = false; // even
Break;
Case '8 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = false; // even
Leftsideparity [2] = true; // odd
Leftsideparity [3] = false; // even
Leftsideparity [4] = false; // even
Leftsideparity [5] = true; // odd
Break;
Case '9 ':
Leftsideparity [0] = true; // odd
Leftsideparity [1] = false; // even
Leftsideparity [2] = false; // even
Leftsideparity [3] = true; // odd
Leftsideparity [4] = false; // even
Leftsideparity [5] = true; // odd
Break;
}
// Second number system digit + 5 symbol manufacter code
String lines = "";
For (INT I = 0; I <6; I ++ ){
Bool oddparity = leftsideparity [I];
If (oddparity ){
Switch (symbols [I + 1])
{
Case '0 ':
Lines + = "0001101 ";
Break;
Case '1 ':
Lines + = "0011001 ";
Break;
Case '2 ':
Lines + = "0010011 ";
Break;
Case '3 ':
Lines + = "0111101 ";
Break;
Case '4 ':
Lines + = "0100011 ";
Break;
Case '5 ':
Lines + = "0110001 ";
Break;
Case '6 ':
Lines + = "0101111 ";
Break;
Case '7 ':
Lines + = "0111011 ";
Break;
Case '8 ':
Lines + = "0110111 ";
Break;
Case '9 ':
Lines + = "0001011 ";
Break;
}
}
// Even parity
Else {
Switch (symbols [I + 1])
{
Case '0 ':
Lines + = "0100111 ";
Break;
Case '1 ':
Lines + = "0110011 ";
Break;
Case '2 ':
Lines + = "0011011 ";
Break;
Case '3 ':
Lines + = "0100001 ";
Break;
Case '4 ':
Lines + = "0011101 ";
Break;
Case '5 ':
Lines + = "0111001 ";
Break;
Case '6 ':
Lines + = "0000101 ";
Break;
Case '7 ':
Lines + = "0010001 ";
Break;
Case '8 ':
Lines + = "0001001 ";
Break;
Case '9 ':
Lines + = "0010111 ";
Break;
}
}
}
// Paint human readable left-side 6 symbol code
G. drawstring (barcode. substring (1,6), Font, new solidbrush (color. Black), X, drawbounds. Y + drawbounds. Height-12 );
Char [] xxx = lines. tochararray ();
For (INT I = 0; I <XXX. length; I ++ ){
If (XXX [I] = '1 '){
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height-12 );
}
Else {
G. drawline (new pen (color. White, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height-12 );
}
X + = linewidth;
}
// Paint center 'Guard Bars', always same '000000'
G. drawline (new pen (color. White, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. White, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. White, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
// 5 symbol product code + 1 Symbol parity
Lines = "";
For (INT I = 7; I <13; I ++ ){
Switch (symbols [I])
{
Case '0 ':
Lines + = "1110010 ";
Break;
Case '1 ':
Lines + = "1100110 ";
Break;
Case '2 ':
Lines + = "1101100 ";
Break;
Case '3 ':
Lines + = "1000010 ";
Break;
Case '4 ':
Lines + = "1011100 ";
Break;
Case '5 ':
Lines + = "1001110 ";
Break;
Case '6 ':
Lines + = "1010000 ";
Break;
Case '7 ':
Lines + = "1000100 ";
Break;
Case '8 ':
Lines + = "1001000 ";
Break;
Case '9 ':
Lines + = "1110100 ";
Break;
}
}
// Paint human readable left-side 6 symbol code
G. drawstring (barcode. substring (7,6), Font, new solidbrush (color. Black), X, drawbounds. Y + drawbounds. Height-12 );
Xxx = lines. tochararray ();
For (INT I = 0; I <XXX. length; I ++ ){
If (XXX [I] = '1 '){
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height-12 );
}
Else {
G. drawline (new pen (color. White, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height-12 );
}
X + = linewidth;
}
// Paint right 'Guard Bars', always same '000000'
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. White, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
X + = linewidth;
G. drawline (new pen (color. Black, linewidth), X, drawbounds. Y, X, drawbounds. Y + drawbounds. Height );
}
}
}