asp.net (C #) Generate Barcode Code39 Barcode Generation Method _ Practical Tips

Source: Internet
Author: User
These days has been to get 128 yards of things, find the relevant information, also did not find. Later no way can only be changed to Code39 barcode. Now write it out and share it with you.

1. First download a free code39 barcode font
2. Build a class for Code39 and write the following code
Copy Code code as follows:

public sealed class Code39
{
#region Private variables
<summary>
The spaces Between each of the Title, BarCode, barcodestring
</summary>
Private Const int space_height = 3;
SizeF _sizelabel = Sizef.empty;
SizeF _sizebarcodevalue = Sizef.empty;
SizeF _sizebarcodestring = Sizef.empty;
#endregion
#region Label
private string _label = null;
Private Font _labelfont = null;
<summary>
BarCode Title (Barcode label)
</summary>
public string Label
{
set {_label = value;}
}
<summary>
BarCode Title Font (font used for barcode labels)
</summary>
Public Font Labelfont
{
Get
{
if (_labelfont = null)
return new Font ("Arial", 10);
return _labelfont;
}
set {_labelfont = value;}
}
#endregion
private string _additionalinfo = null;
Private Font _addtionalinfofont = null;
<summary>
Additional Info Font (additional information font)
</summary>
Public Font Additionalinfofont
{
set {_addtionalinfofont = value;}
Get
{
if (_addtionalinfofont = null) return new Font ("Arial", 10);
return _addtionalinfofont;
}
}
<summary>
Additional info Content, if "Showbarcodevalue" is true, the info is unvisible
Additional information, if the setting Showbarcodevalue is true, this item does not display
</summary>
public string AdditionalInfo
{
set {_additionalinfo = value;}
}
#region BarCode Value and Font
private string _barcodevalue = null;
<summary>
BarCode value (bar code values)
</summary>
public string Barcodevalue
{
Get
{
if (string. IsNullOrEmpty (_barcodevalue))
throw new NullReferenceException ("The Barcodevalue has not been set yet!");
return _barcodevalue;
}
set {_barcodevalue = value. StartsWith ("*") && value. EndsWith ("*")? Value: "*" + value + "*"; }
}
private bool _showbarcodevalue = false;
<summary>
Whether to show the original string of barcode value bellow the barcode
Whether to display bar code values below bar codes, default to False
</summary>
public bool Showbarcodevalue
{
set {_showbarcodevalue = value;}
}
Private Font _barcodevaluefont = null;
<summary>
The font of the codestring to show
Font for barcode values displayed below bar codes
</summary>
Public Font Barcodevaluefont
{
Get
{
if (_barcodevaluefont = null)
return new Font ("Arial", 10);
return _barcodevaluefont;
}
set {_barcodevaluefont = value;}
}
private int _barcodefontsize = 50;
<summary>
The font size of the barcode value to draw
Size of bar code drawing, default 50
</summary>
public int Barcodefontszie
{
set {_barcodefontsize = value;}
}
#endregion
#region Generate the barcode image
Private Bitmap Blankbackimage
{
Get
{
int barcodewidth = 0, barcodeheight = 0;
using (Bitmap BMP = new Bitmap (1, 1, PIXELFORMAT.FORMAT32BPPARGB))
{
using (Graphics g = graphics.fromimage (BMP))
{
if (!string. IsNullOrEmpty (_label))
{
_sizelabel = g.measurestring (_label, Labelfont);
Barcodewidth = (int) _sizelabel.width;
Barcodeheight = (int) _sizelabel.height + space_height;
}
_sizebarcodevalue = g.measurestring (Barcodevalue, New Font ("Free 3 of 9 Extended", _barcodefontsize));
Barcodewidth = Math.max (barcodewidth, (int) _sizebarcodevalue.width);
Barcodeheight + = (int) _sizebarcodevalue.height;
if (_showbarcodevalue)
{
_sizebarcodestring = g.measurestring (_barcodevalue, Barcodevaluefont);
Barcodewidth = Math.max (barcodewidth, (int) _sizebarcodestring.width);
Barcodeheight + + (int) _sizebarcodestring.height + space_height;
}
Else
//{
if (!string. IsNullOrEmpty (_additionalinfo))
// {
_sizeadditionalinfo = g.measurestring (_additionalinfo, Additionalinfofont);
Barcodewidth = Math.max (barcodewidth, (int) _sizeadditionalinfo.width);
Barcodeheight + + (int) _sizeadditionalinfo.height + space_height;
// }
//}
}
}
return new Bitmap (Barcodewidth, Barcodeheight, Pixelformat.format32bppargb);
}
}
<summary>
Draw the barcode value to the blank back image and output it to the browser
Draw a barcode in webform form
</summary>
<param name= "MS" >recommand the "Response.outputstream" using response.outputstream</param>
<param name= "ImageFormat" >the image format to the Browser output to browser to picture formats, recommended gif</param>
Public Bitmap Createwebform (Stream MS, ImageFormat ImageFormat)
{
int Barcodewidth, barcodeheight;
using (Bitmap BMP = this. Blankbackimage)
{
Barcodeheight = BMP. Height;
Barcodewidth = BMP. Width;
using (Graphics g = graphics.fromimage (BMP))
{
G.clear (Color.White);
G.textrenderinghint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
int vpos = 0;
Draw Label String
if (!string. IsNullOrEmpty (_label))
{
g.DrawString (_label, Labelfont, New SolidBrush (Color.Black),
Xcenter ((int) _sizelabel.width, barcodewidth), vPOS);
vPOS + + (int) _sizelabel.height + space_height;
}
else {vpos = space_height;}
Draw the Bar Value
g.DrawString (_barcodevalue, New Font ("Free 3 of 9 Extended", _barcodefontsize), New SolidBrush (Color.Black),
Xcenter ((int) _sizebarcodevalue.width, barcodewidth), vPOS);
Draw the Barvalue String
if (_showbarcodevalue)
{
g.DrawString (_barcodevalue, Barcodevaluefont, New SolidBrush (Color.Black),
Xcenter ((int) _sizebarcodestring.width, barcodewidth),
vPOS + (int) _sizebarcodevalue.height);
}
Else
//{
if (!string. IsNullOrEmpty (_additionalinfo))
// {
g.DrawString (_additionalinfo, Additionalinfofont, New SolidBrush (Color.Black),
Xcenter ((int) _sizeadditionalinfo.width, barcodewidth),
vPOS + (int) _sizebarcodevalue.height);
// }
//}
}
Bmp. Save (MS, ImageFormat);
return BMP;
}
}
<summary>
Generate barcode in WinForm format
</summary>
<param name= "ImageFormat" > Picture format, recommended gif</param>
<returns>stream type </returns>
Public Stream Createwinform (ImageFormat imageformat)
{
int Barcodewidth, barcodeheight;
using (Bitmap BMP = this. Blankbackimage)
{
Barcodeheight = BMP. Height;
Barcodewidth = BMP. Width;
using (Graphics g = graphics.fromimage (BMP))
{
G.clear (Color.White);
G.textrenderinghint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
int vpos = 0;
Draw Label String
if (!string. IsNullOrEmpty (_label))
{
g.DrawString (_label, Labelfont, New SolidBrush (Color.Black),
Xcenter ((int) _sizelabel.width, barcodewidth), vPOS);
vPOS + + (int) _sizelabel.height + space_height;
}
else {vpos = space_height;}
Draw the Bar Value
g.DrawString (_barcodevalue, New Font ("Free 3 of 9 Extended", _barcodefontsize), New SolidBrush (Color.Black),
Xcenter ((int) _sizebarcodevalue.width, barcodewidth), vPOS);
Draw the Barvalue String
if (_showbarcodevalue)
{
g.DrawString (_barcodevalue, Barcodevaluefont, New SolidBrush (Color.Black),
Xcenter ((int) _sizebarcodestring.width, barcodewidth),
vPOS + (int) _sizebarcodevalue.height);
}
Else
//{
if (!string. IsNullOrEmpty (_additionalinfo))
// //{
g.DrawString (_additionalinfo, Additionalinfofont, New SolidBrush (Color.Black),
//xcenter ((int) _sizeadditionalinfo.width, barcodewidth),
vPOS + (int) _sizebarcodevalue.height);
// //}
//}
}
Stream ms = new MemoryStream ();
Bmp. Save (MS, ImageFormat);
return MS;
}
}
#endregion
private static int xcenter (int subwidth, int globalwidth)
{
Return (Globalwidth-subwidth)/2;
}
}

3. If it is a Web program please call Createwebform if it is a CS program use Createwinform
4. Create a new ASPX file and write the following code
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Code39 code Code39 = new ();
Code39. Barcodevalue = "LDSO-001";
Code39. Barcodefontszie = 60;
//Code39. Label = "39 yards, bottom display code value";
Code39. Showbarcodevalue = true;
Response.Write (Code39. Createwebform (Response.outputstream, System.Drawing.Imaging.ImageFormat.Gif));
Code39 = null;
}
Related Article

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.