欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-06 13:55:08
这几天一直在弄128条码的事情,找了相关的资料,也没找到。后来没办法只能改成code39的条码。现在把它写出来,与大家分享1.先下载一种免费的 code39条码字体 2.建...
这几天一直在弄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;
// }
//}
}
}
return new bitmap(barcodewidth, barcodeheight, pixelformat.format32bppargb);
}
}
/// <summary>
/// draw the barcode value to the blank back image and output it to the browser
/// 绘制webform形式的条码
/// </summary>
/// <param name="ms">recommand the "response.outputstream" 使用 response.outputstream</param>
/// <param name="imageformat">the image format to the browser 输出到浏览器到图片格式,建议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>
/// 生成winform格式的条码
/// </summary>
/// <param name="imageformat">图片格式,建议gif</param>
/// <returns>stream类型</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.如果是web程序 请调用 createwebform 如果是cs程序 则使用createwinform
4.新建一aspx文件,写入以下代码
复制代码 代码如下:

protected void page_load(object sender, eventargs e)
{
code39 code39 = new code39();
code39.barcodevalue = "ldso-001";
code39.barcodefontszie = 60;
// code39.label = "39码,底部显示码值";
code39.showbarcodevalue = true;
response.write(code39.createwebform(response.outputstream, system.drawing.imaging.imageformat.gif));
code39 = null;
}