ASP.net (c #) code generation code39 Code Generation Method

Source: Internet
Author: User

I have been working on code 128 in the past few days. I have not found any relevant materials. Later, it was impossible to change the code to code39. Write it out and share it with you.

1. First download a free code39 barcode font
2. Create a class named code39 and write the following code
Copy codeThe Code is as follows:
Public sealed class Code39
{
# Region private variables
/// <Summary>
/// The Space Between each of 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 (bar code label)
/// </Summary>
Public string Label
{
Set {_ label = value ;}
}
/// <Summary>
/// BarCode Title Font (Font used for bar code 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 ShowBarCodeValue is set to true, this item is not displayed.
/// </Summary>
Public string AdditionalInfo
{
Set {_ additionalInfo = value ;}
}
# Region BarCode Value and Font
Private string _ barCodeValue = null;
/// <Summary>
/// BarCode Value (bar code Value)
/// </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 the bar code value is displayed below the bar code. The default value is false.
/// </Summary>
Public bool ShowBarCodeValue
{
Set {_ showBarCodeValue = value ;}
}
Private Font _ barCodeValueFont = null;
/// <Summary>
/// The font of the codestring to show
/// Font of the bar code value displayed below the bar code
/// </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
/// The bar code size. The default value is 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 WebForm bar code
/// </Summary>
/// <Param name = "ms"> Recommand the "Response. OutputStream" using Response. OutputStream </param>
/// <Param name = "imageFormat"> The Image format to the Browser is output to The Image format in the Browser. GIF is recommended. </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 a winform bar code
/// </Summary>
/// <Param name = "imageFormat"> image format. GIF is recommended. </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, call CreateWebForm. If it is a cs program, use CreateWinForm.
4. Create An aspx file and write the following code
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Code39 code39 = new Code39 ();
Code39.BarCodeValue = "LDSO-001 ";
Code39.BarCodeFontSzie = 60;
// Code39.Label = "39 yards, the code value is displayed at the bottom ";
Code39.ShowBarCodeValue = true;
Response. Write (code39.CreateWebForm (Response. OutputStream, System. Drawing. Imaging. ImageFormat. Gif ));
Code39 = null;
}

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.