这几天一直在弄128条码的事情,找了相关的资料,也没找到。后来没办法只能改成code39的条码。现在把它写出来,与大家分享

1.先下载一种免费的 code39条码字体   点击这里也可下载

2.建个类 为 code39 并写入以下代码

代码

publicsealedclass Code39
{
#region private variables
///<summary>
/// The Space Between each of Title, BarCode, BarCodeString
///</summary>
privateconstint SPACE_HEIGHT =3;
SizeF _sizeLabel = SizeF.Empty;
SizeF _sizeBarCodeValue = SizeF.Empty;
SizeF _sizeBarCodeString = SizeF.Empty;

#endregion

#region Label
privatestring _label =null;
private Font _labelFont =null;
///<summary>
/// BarCode Title (条码标签)
///</summary>
publicstring Label
{
set { _label = value; }
}
///<summary>
/// BarCode Title Font (条码标签使用的字体)
///</summary>
public Font LabelFont
{
get
{
if (_labelFont ==null)
returnnew Font("Arial", 10);
return _labelFont;
}
set { _labelFont = value; }
}
#endregion

privatestring _additionalInfo =null;
private Font _addtionalInfoFont =null;
///<summary>
/// Additional Info Font (附加信息字体)
///</summary>
public Font AdditionalInfoFont
{
set { _addtionalInfoFont = value; }
get
{
if (_addtionalInfoFont ==null) returnnew Font("Arial", 10);
return _addtionalInfoFont;
}
}
///<summary>
/// Additional Info Content, if "ShowBarCodeValue" is true, the info is unvisible
/// 附加信息,如果设置ShowBarCodeValue为true,则此项不显示
///</summary>
publicstring AdditionalInfo
{
set { _additionalInfo = value; }
}

#region BarCode Value and Font
privatestring _barCodeValue =null;
///<summary>
/// BarCode Value (条码值)
///</summary>
publicstring BarCodeValue
{
get
{
if (string.IsNullOrEmpty(_barCodeValue))
thrownew NullReferenceException("The BarCodeValue has not been set yet!");
return _barCodeValue;
}
set { _barCodeValue = value.StartsWith("*") && value.EndsWith("*") ? value : "*"+ value +"*"; }
}

privatebool _showBarCodeValue =false;
///<summary>
/// whether to show the original string of barcode value bellow the barcode
/// 是否在条码下方显示条码值,默认为false
///</summary>
publicbool ShowBarCodeValue
{
set { _showBarCodeValue = value; }
}

private Font _barCodeValueFont =null;
///<summary>
/// the font of the codestring to show
/// 条码下方显示的条码值的字体
///</summary>
public Font BarCodeValueFont
{
get
{
if (_barCodeValueFont ==null)
returnnew Font("Arial", 10);
return _barCodeValueFont;
}
set { _barCodeValueFont = value; }
}

privateint _barCodeFontSize =50;
///<summary>
/// the font size of the barcode value to draw
/// 条码绘制的大小,默认50
///</summary>
publicint 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;
// }
//}
}
}

returnnew 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

privatestaticint XCenter(int subWidth, int globalWidth)
{
return (globalWidth - subWidth) /2;
}

}

3.如果是web程序 请调用 CreateWebForm 如果是cs程序 则使用CreateWinForm

4.新建一aspx文件,写入以下代码

代码

protectedvoid 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;
}

顺便把源代码也给奉上吧

作者:chenhuzi   出处:http://www.cnblogs.com/chenhuzi
本文版权归作者所有 欢迎转载 但未经作者同意必须保留此段声明 且在文章页面明显位置给出原文连接 否则保留追究法律责任的权利
个人淘宝站:http://chenhuzi.taobao.com 个人网站:http://chenhuzi.gicp.net

转载于:https://www.cnblogs.com/mrray/archive/2012/08/08/2628316.html

C# asp.net 条形码 code39条码 生成 扫描枪能识别(好东西转载...)相关推荐

  1. EXCEL直接生成扫描枪可识别条码

    EXCEL直接生成扫描枪可识别条码 由于excel中直接把字体改成code128条码类型之后虽然可以显示条码,但是打印出来扫描枪是无法扫描识别的.但是在EXCEL2010中制作条形码十分方便,可以 ...

  2. 1D和2D条码生成、检测、识别控件Barcode Xpress

    Barcode Xpress是一款优秀的.高速的1D和2D条码生成.检测.识别控件,可以把条码放置在页面任何位置,支持Code 39 和128,UPC, EAN,4-state postal code ...

  3. 安卓,Android,Scanner Gun,Barcode Scanner,条码,扫描枪,二维码,键盘模式

    安卓对接扫码枪,扫码枪模拟键盘输入 方案一 在页面添加个隐藏的input 框,在input框内获取值,来得到扫码枪的值. 获取焦点,软键盘弹出不易隐藏. 方案二 标准扫描枪扫描数据会触发KEYCODE ...

  4. 扫码枪回车键条码_扫描枪怎么设置自动换行 条码扫描枪不自动回车怎么设置...

    扫码枪设置自动回车方法步骤 一般的扫描枪在出厂默认模式时是带有回车的,可是有些特殊情况下即使客户恢复出厂设置,扫描条码还是无法进行换行.这种情况一般是由于PC端有一些特殊的驱动对扫描枪的数据传输造成干 ...

  5. 速卖通打印标签快递单条形码太细小导致打印出来的条形码挤在一起,扫描枪无法识别,如何打印出清晰的条形码

    所有东西的原则都是慢工出细活的,打印机也一样,除非是价格昂贵的机器,一般普通的热敏打印机,高速打印是无法满足精度要求的,速卖通打印标签快递单条形码太小的话,即要求打印机要打出精度高的条形码.这时可以通 ...

  6. 条码生成代码(Code39码)

    zryou 的 条码生成代码(Code39码) 『Code 39条形码介绍』 code39条码能表示字母.数字和其它一些符号共43个字符:A -Z, 0-9, -.$/ + %, space等,其主要 ...

  7. 水晶报表2008 条码打印 扫描

    前几天项目上用到了水晶报表的条码打印功能 最开始用的字体,code39 code128都试了,可以正常打印,但是扫描枪不识别, 据说code39值两边加*可以扫描到,我没能成功 又在网上找到以下方法, ...

  8. java生成带星号条形码_Code39生成条形码加星号的解决办法以及当扫描枪扫描不到条码的解决办法 | 学步园...

    这里还是引用一段别人的代码: using System; using System.Collections; using System.ComponentModel; using System.Dra ...

  9. APIcoud 手机二维码or条码 生成与扫描模块

    APIcoud 手机二维码or条码 生成与扫描模块 刚接触APIcoud 的时候觉得很多模块真的好难,初次接触二维码的时候觉得,生成二维码真的很费劲呢,其实不然,是真的很难,但是APIcoud 已经封 ...

最新文章

  1. Paper2:Fast 3D Line Segment Detection From Unorganized Point Cloud
  2. 深聪智能朱澄宇:自研 AI 芯片不是赶时髦 | CCF-GAIR 2019
  3. 转:Object-Runtime的基本数据类型
  4. 1088. [SCOI2005]扫雷Mine【网格DP】
  5. 论文浅尝 | 利用边缘标签的网络嵌入强化方法
  6. 生活在别处——“Samsung Cloud Print”云打印体验
  7. PAT:1001. 害死人不偿命的(3n+1)猜想 (15) AC
  8. vue-webpack项目本地开发环境设置代理解决跨域问题
  9. linux 下显卡优化,[转载]Linux 下 NVIDIA 显卡闭源驱动的一些优化
  10. mysql用jdbc执行回滚吗_java – mysql jdbc不成功回滚
  11. Axure RP9授权码(亲测有效)
  12. avr单片机c语言大小比较,AVR单片机C语言入门知识
  13. 读《显微镜下的大明》马伯庸---笔记
  14. 如何用Python计算周数
  15. 【教程篇】手机卡刷Rom详细教程
  16. 作业帮面经(已拿offer)
  17. 大数据分析实战之异构数据源联合分析业务创新实践
  18. 手机NFC开启门禁方案介绍
  19. zigbee入网过程深入解析(Ubiqua抓包)
  20. DCloud 使用chrome调试

热门文章

  1. 苹果参加618,押注天猫首战5小时卖出5亿,天猫已成行业3C领导者
  2. 程序员课外拓展007:强烈推荐一个自学网:可汗学院
  3. 人生遐思:所谓的人生到底是什么呢
  4. linux系统命令make.clean的用法讲解
  5. Git add 、commit后报错:nothing to commit, working tree clean
  6. Clickhouse库引擎介绍
  7. windows环境搭建MQTT
  8. 十一、jQuery(下) - 章节课后练习题及答案
  9. XiaoHu.ai开发日志(自2018年2月6日至2019年4月11日)
  10. java接入顺丰快递api(有可运行demo)(包括下单、路由查询、路由推送、查询订单状态、取消订单)