How to use C # To draw 39 bar codes

Source: Internet
Author: User

 

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;

Namespace CjLibrary
{

Public enum AlignType
{
Left = 0,
Right = 1,
Center = 2
}

Public class CjBarCode
{
Private AlignType align;

Public AlignType Align
{
Get {return align ;}
Set {Align = value ;}
}
Private int leftmargin = 10;

Public int leftmargin
{
Get {return leftmargin ;}
Set {leftmargin = value ;}
}
Private int topmargin = 10;

Public int topmargin
{
Get {return topMargin ;}
Set {topMargin = value ;}
}
Private int barcodeHeight = 50;

Public int BarcodeHeight
{
Get {return barcodeHeight ;}
Set {barcodeHeight = value ;}
}

Private string headerText = "Shaoxing, Zhejiang Province ";

Public string HeaderText
{
Get {return headerText ;}
Set {headerText = value ;}
}

Private string barcodeText = "";

Public string BarcodeText
{
Get {return barcodeText ;}
Set {barcodeText = value ;}
}

Private bool showheader;

Public bool showheader
{
Get {return showheader ;}
Set {showheader = value ;}
}

Private bool showfooter;

Public bool showfooter
{
Get {return showfooter ;}
Set {showfooter = value ;}
}


Private int headerfontsize;

Public int headerfontsize
{
Get {return headerfontsize ;}
Set {headerfontsize = value ;}
}
Private int footerfontsize;

Public int footerfontsize
{
Get {return footerfontsize ;}
Set {footerFontSize = value ;}
}
Private string headerFontFamily;

Public string HeaderFontFamily
{
Get {return headerFontFamily ;}
Set {headerFontFamily = value ;}
}
Private string footerFontFamily;

Public string FooterFontFamily
{
Get {return footerfontfamily ;}
Set {footerfontfamily = value ;}
}

Protected font headerfont = NULL;
Protected font footerfont = NULL;

Public cjbarcode ()
{
Align = aligntype. Center;
Headerfontfamily = "Courier ";
FooterFontFamily = "Courier ";
HeaderFontSize = 9;
FooterFontSize = 9;
HeaderFont = new Font (headerFontFamily, headerFontSize );
FooterFont = new Font (footerFontFamily, footerFontSize );

}

Protected virtual void DrawBitmap (Graphics g, Rectangle rects)
{

SolidBrush brush = new SolidBrush (Color. PowderBlue );
G. FillRectangle (brush, rects );
}
Protected virtual void DrawText (string text, Graphics g, Rectangle rects)
{
SolidBrush brush = new SolidBrush (Color. PowderBlue );
G. FillRectangle (brush, rects );
}

}

Public class CjBarCode39: CjBarCode
{

Private double wideToNarrowRatio = 3.0;

Public double WideToNarrowRatio
{
Get {return wideToNarrowRatio ;}
Set {wideToNarrowRatio = value ;}
}
Private int weight = 1;

Public int Weight
{
Get {return weight ;}
Set {weight = value ;}
}
/// <Summary>
/// 39 characters in the bar code
/// </Summary>
Private String alphabet39 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+ % *";

Private String [] coded39Char =
{
/* 0 */"001100100 ",
/* 1 */"100010100 ",
/* 2 */"010010100 ",
/* 3 x/"110000100 ",
/X 4 */"001010100 ",
/X 5 */"101000100 ",
/* 6 x/"011000100 ",
/* 7 x/"000110100 ",
/X 8 */"100100100 ",
/X 9 */"010100100 ",
/* A */"100010010 ",
/* B */"010010010 ",
/* C */"110000010 ",
/* D x/"001010010 ",
/* E/"101000010 ",
/* F */"011000010 ",
/* G/"000110010 ",
/* H */"100100010 ",
/* I/*/"010100010 ",
/* J */"001100010 ",
/X k */"100010001 ",
/* L */"010010001 ",
/* M */"110000001 ",
/* N */"001010001 ",
/* O */"101000001 ",
/* P */"011000001 ",
/* Q */"000110001 ",
/* R */"100100001 ",
/X s */"010100001 ",
/* T */"001100001 ",
/* U */"100011000 ",
/X v */"010011000 ",
/* W */"110001000 ",
/* X */"001011000 ",
/* Y x/"101001000 ",
/* Z */"011001000 ",
/*-*/"000111000 ",
/*. */"110000100 ",
/* ''' */"011000100 ",
/** $*/"010101000 ",
/*/"010100010 ",
/* + */"010001010 ",
/* % */"100101000 ",
/****/"001101000"
};
Public cjbarcode39 ()
{
Barcodetext = "1234 ";
}
/// <Summary>
/// Calculate the left and top coordinates of the bar code to center the bar code.
/// </Summary>
/// <Returns> </returns>
Private int getx ()
{
Int currentlocation = 0;
String strbarcode = "*" + barcodetext. toupper () + "*";
For (INT I = 0; I <strbarcode. length; I ++)
{
String encodedstring = coded39char [alphabet39.indexof (strbarcode [I])];

For (Int J = 0; j <5; j ++)
{

If (encodedString [j] = '0 ')
{
CurrentLocation + = weight;
}
Else
{
CurrentLocation + = 3 * weight;
}
// Draw 6th white stripes
If (j + 5) <9)
{
If (encodedString [j + 5] = '0 ')
{
Currentlocation + = weight;
}
Else
{
Currentlocation + = 3 * weight;
}
}
}
Currentlocation + = weight;
}
Return currentlocation;
}
/// <Summary>
/// Display the barcode
/// </Summary>
/// <Param name = "g"> GDI + </param>
/// <Param name = "rects"> drawing area </param>
Protected override void DrawBitmap (Graphics g, Rectangle rects)
{
If (BarcodeText = "") return;
String strBarcode = "*" + BarcodeText. ToUpper () + "*";
// String strbarcode = barcodetext. toupper ();
String encodedstring = "";
Int currentlocation = (rects. Width-getx ()/2;
Solidbrush blackbrush = new solidbrush (color. Black );
Solidbrush witebrush = new solidbrush (color. White );
Int ytop = rects. Y;
For (INT I = 0; I <strbarcode. length; I ++)
{
Encodedstring = coded39char [alphabet39.indexof (strbarcode [I])];

For (Int J = 0; j <5; j ++)
{

If (encodedstring [J] = '0 ')
{
Rectangle RE1 = new rectangle (currentlocation, ytop, weight, barcodeheight );
G. fillrectangle (blackbrush, RE1 );
Currentlocation + = weight;
}
Else
{
Rectangle re1 = new Rectangle (currentLocation, yTop, 3 * weight, BarcodeHeight );
G. FillRectangle (blackBrush, re1 );
CurrentLocation + = 3 * weight;
}
// Draw 6th white stripes
If (J + 5) <9)
{
If (encodedstring [J + 5] = '0 ')
{
Rectangle RE1 = new rectangle (currentlocation, ytop, weight, barcodeheight );
G. fillrectangle (witebrush, RE1 );
Currentlocation + = weight;
}
Else
{
Rectangle RE1 = new rectangle (currentlocation, ytop, 3 * weight, barcodeheight );
G. fillrectangle (witebrush, RE1 );
Currentlocation + = 3 * weight;
}
}
}
Rectangle re2 = new Rectangle (currentLocation, yTop, weight, BarcodeHeight );
G. FillRectangle (witeBrush, re2 );
CurrentLocation + = weight;
}

}

/// <Summary>
/// Display the bar code and text
/// </Summary>
/// <Param name = "G"> </param>
/// <Param name = "rects"> </param>
Public void drawbarcode (Graphics g, rectangle rects)
{
Sizef hsize = G. measurestring (headertext, this. headerfont );
Sizef fsize = G. measurestring (barcodetext, this. footerfont );
// Headtext
Rectangle T = new rectangle ();
T = rects;
T. Height = (INT) hsize. height;
T. X = (rects. Width-(INT) hsize. width)/2;
Drawtext (headertext, G, T );
// Foottext
Rectangle B = new rectangle ();
B = rects;
B. Y = rects. Height-(INT) fsize. height;
B. x = (rects. Width-(INT) fsize. width)/2;
B. Height = (int) fsize. Height;
DrawText ("*" + BarcodeText + "*", g, B );
// BarCode
Rectangle m = new Rectangle ();
M = rects;
M. Y = t. Height;
M. Height = rects. Height-t. Height-B. Height;
This. BarcodeHeight = m. Height;
DrawBitmap (g, m );
}
/// <Summary>
/// Text display
/// </Summary>
/// <Param name = "text"> </param>
/// <Param name = "g"> </param>
/// <Param name = "rects"> </param>
Protected override void DrawText (string text, Graphics g, Rectangle rects)
{
G. DrawString (text, this. headerFont, Brushes. Black, rects );
}

 

}
}

 

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.