ASP.net(c#)生成条形码 code39条码生成方法

编程学习 2021-07-04 22:40www.dzhlxh.cn编程入门
这几天一直在弄128条码的事情,找了相关的资料,也没找到。后来没办法只能改成code39的条码,需要的朋友可以参考下
这几天一直在弄128条码的事情,找了相关的资料,也没找到。后来没办法只能改成code39的条码。现在把它写出来,与大家分享

1.先下载一种免费的 code39条码字体
2.建个类 为 code39 并写入以下代码
代码如下:

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 (条码标签)
/// </summary>
public string Label
{
set { _label = value; }
}
/// <summary>
/// BarCode Title Font (条码标签使用的字体)
/// </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 (附加信息字体)
/// </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
/// 附加信息,如果设置ShowBarCodeValue为true,则此项不显示
/// </summary>
public string AdditionalInfo
{
set { _additionalInfo = value; }
}
#region BarCode Value and Font
private string _barCodeValue = null;
/// <summary>
/// BarCode 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
/// 是否在条码下方显示条码值,默认为false
/// </summary>
public bool ShowBarCodeValue
{
set { _showBarCodeValue = value; }
}
private Font _barCodeValueFont = null;
/// <summary>
/// the font of the codestring to show
/// 条码下方显示的条码值的字体
/// </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
/// 条码绘制的大小,默认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;
// }
/

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板