史上最简洁C# 生成条形码图片思路及示例分享
程序员文章站
2023-12-13 22:27:16
在网上看到一些人写关于条形码的代码都很长,有的甚至拿来卖,所以查了下资料,希望能对大家有帮助。
我的实现原理是:
其实windows本身就有一个字体是用来显示条...
在网上看到一些人写关于条形码的代码都很长,有的甚至拿来卖,所以查了下资料,希望能对大家有帮助。
我的实现原理是:
其实windows本身就有一个字体是用来显示条形码的。
只要将数字改为这种字体就变成了条形码。
windows字体库下,有如下八种字体可以用来将数字转换成条形码:
复制代码 代码如下:
code39azaleanarrow1
code39azaleanarrow2
code39azaleanarrow3
code39azalearegular1
code39azalearegular2
code39azaleawide1
code39azaleawide2
code39azaleawide3
把代码贴给大家参考:
复制代码 代码如下:
bitmap b=new bitmap(200,200);
graphics g = graphics.fromimage(b);
font font = new font("code39azalearegular2", 32);
g.drawstring("123456", font, brushes.black, new pointf(100,100));
picturebox1.backgroundimage = b;
picturebox1.backgroundimagelayout = imagelayout.zoom。
是不是很简单呢,亲们,思路给大家了,具体使用到项目中的时候,小伙伴们自己扩展下就可以了。
另附上一则示例中的应用
复制代码 代码如下:
using system;
using system.collections.generic;
using system.web;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;
using system.drawing.text;
using system.drawing.design;
using system.io;
using system.collections;
namespace public.equipment
{
/// <summary>
///绘制条形码
/// </summary>
public class drawingbarcode
{
public drawingbarcode()
{
//
//todo: 在此处添加构造函数逻辑
//
}
#region 根据字体产生条形码
/// <summary>
/// 根据条形码绘制图片
/// </summary>
/// <param name="strnumber">条形码</param>
public void drawingbarcode(string strnumber)
{
privatefontcollection fonts = new privatefontcollection();
//39带数字
//fonts.addfontfile(httpcontext.current.server.mappath(".") + "/barcodefonts/v100010_.ttf");
//fontfamily ff = new fontfamily("c39hrp48dltt", fonts);
//39码
strnumber = "*" + strnumber + "*";
fonts.addfontfile(httpcontext.current.server.mappath(".") + "/barcodefonts/free3of9x.ttf");
fontfamily ff = new fontfamily("free 3 of 9 extended", fonts);
//接近条形码
//fonts.addfontfile(httpcontext.current.server.mappath(".") + "/barcodefonts/v100014_.ttf");
//fontfamily ff = new fontfamily("c39p24dltt", fonts);
font font = new font(ff, 12);
//设置图片大小
image img = new bitmap(1, 1);
graphics g = graphics.fromimage(img);
sizef fontsize = g.measurestring(strnumber, font);
int intwidth = convert.toint32(fontsize.width);
int intheight = convert.toint32(fontsize.height);
g.dispose();
img.dispose();
img = new bitmap(intwidth, intheight);
g = graphics.fromimage(img);
g.clear(color.white);
g.compositingquality = compositingquality.highquality;
g.smoothingmode = smoothingmode.highquality;
g.drawstring(strnumber, font, brushes.black, 0, 0);
memorystream stream = new memorystream();
img.save(stream, imageformat.jpeg);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype = "image/jpeg";
httpcontext.current.response.binarywrite(stream.toarray());
g.dispose();
img.dispose();
}
/// <summary>
/// 根据条形码绘制图片
/// </summary>
/// <param name="strnumber">条形码</param>
/// <param name="intfontsize">字体大小</param>
public void drawingbarcode(string strnumber,int intfontsize)
{
privatefontcollection fonts = new privatefontcollection();
//39带数字
//fonts.addfontfile(httpcontext.current.server.mappath(".") + "/barcodefonts/v100010_.ttf");
//fontfamily ff = new fontfamily("c39hrp48dltt", fonts);
//39码
strnumber = "*" + strnumber + "*";
fonts.addfontfile(httpcontext.current.server.mappath(".") + "/barcodefonts/free3of9x.ttf");
fontfamily ff = new fontfamily("free 3 of 9 extended", fonts);
//接近条形码
//fonts.addfontfile(httpcontext.current.server.mappath(".") + "/barcodefonts/v100014_.ttf");
//fontfamily ff = new fontfamily("c39p24dltt", fonts);
font font = new font(ff, intfontsize);
//设置图片大小
image img = new bitmap(1, 1);
graphics g = graphics.fromimage(img);
sizef fontsize = g.measurestring(strnumber, font);
int intwidth = convert.toint32(fontsize.width);
int intheight = convert.toint32(fontsize.height);
g.dispose();
img.dispose();
img = new bitmap(intwidth, intheight);
g = graphics.fromimage(img);
g.clear(color.white);
g.compositingquality = compositingquality.highquality;
g.smoothingmode = smoothingmode.highquality;
g.drawstring(strnumber, font, brushes.black, 0, 0);
memorystream stream = new memorystream();
img.save(stream, imageformat.jpeg);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype = "image/jpeg";
httpcontext.current.response.binarywrite(stream.toarray());
g.dispose();
img.dispose();
}
#endregion
#region 绘制code39码
/// <summary>
/// 根据条形码绘制图片
/// </summary>
/// <param name="strnumber">条形码</param>
public void drawingbarcode39(string strnumber,int intfontsize,bool withstart)
{
viewfont = new font("宋体", intfontsize);
image img = getcodeimage(strnumber, code39model.code39normal, withstart);
memorystream stream = new memorystream();
img.save(stream, imageformat.jpeg);
httpcontext.current.response.clear();
httpcontext.current.response.clearcontent();
httpcontext.current.response.bufferoutput = true;
httpcontext.current.response.contenttype = "image/jpeg";
httpcontext.current.response.binarywrite(stream.getbuffer());
httpcontext.current.response.flush();
}
private hashtable m_code39 = new hashtable();
private byte m_magnify = 0;
/// <summary>
/// 放大倍数
/// </summary>
public byte magnify { get { return m_magnify; } set { m_magnify = value; } }
private int m_height = 40;
/// <summary>
/// 图形高
/// </summary>
public int height { get { return m_height; } set { m_height = value; } }
private font m_viewfont = null;
/// <summary>
/// 字体大小
/// </summary>
public font viewfont { get { return m_viewfont; } set { m_viewfont = value; } }
/**
code39码的编码规则是:
1、 每五条线表示一个字符;
2、 粗线表示1,细线表示0;
3、 线条间的间隙宽的表示1,窄的表示0;
4、 五条线加上它们之间的四条间隙就是九位二进制编码,而且这九位中必定有三位是1,所以称为39码;
5、 条形码的首尾各一个*标识开始和结束
*/
public drawingbarcode(boolean iscode39)
{
m_code39.add("a", "1101010010110");
m_code39.add("b", "1011010010110");
m_code39.add("c", "1101101001010");
m_code39.add("d", "1010110010110");
m_code39.add("e", "1101011001010");
m_code39.add("f", "1011011001010");
m_code39.add("g", "1010100110110");
m_code39.add("h", "1101010011010");
m_code39.add("i", "1011010011010");
m_code39.add("j", "1010110011010");
m_code39.add("k", "1101010100110");
m_code39.add("l", "1011010100110");
m_code39.add("m", "1101101010010");
m_code39.add("n", "1010110100110");
m_code39.add("o", "1101011010010");
m_code39.add("p", "1011011010010");
m_code39.add("q", "1010101100110");
m_code39.add("r", "1101010110010");
m_code39.add("s", "1011010110010");
m_code39.add("t", "1010110110010");
m_code39.add("u", "1100101010110");
m_code39.add("v", "1001101010110");
m_code39.add("w", "1100110101010");
m_code39.add("x", "1001011010110");
m_code39.add("y", "1100101101010");
m_code39.add("z", "1001101101010");
m_code39.add("0", "1010011011010");
m_code39.add("1", "1101001010110");
m_code39.add("2", "1011001010110");
m_code39.add("3", "1101100101010");
m_code39.add("4", "1010011010110");
m_code39.add("5", "1101001101010");
m_code39.add("6", "1011001101010");
m_code39.add("7", "1010010110110");
m_code39.add("8", "1101001011010");
m_code39.add("9", "1011001011010");
m_code39.add("+", "1001010010010");
m_code39.add("-", "1001010110110");
m_code39.add("*", "1001011011010");
m_code39.add("/", "1001001010010");
m_code39.add("%", "1010010010010");
m_code39.add("$", "1001001001010");
m_code39.add(".", "1100101011010");
m_code39.add(" ", "1001101011010");
}
public enum code39model
{
/// <summary>
/// 基本类别 1234567890abc
/// </summary>
code39normal,
/// <summary>
/// 全ascii方式 +a+b 来表示小写
/// </summary>
code39fullascii
}
/// <summary>
/// 获得条码图形
/// </summary>
/// <param name="p_text">文字信息</param>
/// <param name="p_model">类别</param>
/// <param name="p_starchar">是否增加前后*号</param>
/// <returns>图形</returns>
public bitmap getcodeimage(string p_text, code39model p_model, bool p_starchar)
{
string _valuetext = "";
string _codetext = "";
char[] _valuechar = null;
switch (p_model)
{
case code39model.code39normal:
_valuetext = p_text.toupper();
break;
default:
_valuechar = p_text.tochararray();
for (int i = 0; i != _valuechar.length; i++)
{
if ((int)_valuechar[i] >= 97 && (int)_valuechar[i] <= 122)
{
_valuetext += "+" + _valuechar[i].tostring().toupper();
}
else
{
_valuetext += _valuechar[i].tostring();
}
}
break;
}
_valuechar = _valuetext.tochararray();
if (p_starchar == true) _codetext += m_code39["*"];
for (int i = 0; i != _valuechar.length; i++)
{
if (p_starchar == true && _valuechar[i] == '*') throw new exception("带有起始符号不能出现*");
object _charcode = m_code39[_valuechar[i].tostring()];
if (_charcode == null) throw new exception("不可用的字符" + _valuechar[i].tostring());
_codetext += _charcode.tostring();
}
if (p_starchar == true) _codetext += m_code39["*"];
bitmap _codebmp = getimage(_codetext);
getviewimage(_codebmp, p_text);
return _codebmp;
}
/// <summary>
/// 绘制编码图形
/// </summary>
/// <param name="p_text">编码</param>
/// <returns>图形</returns>
private bitmap getimage(string p_text)
{
char[] _value = p_text.tochararray();
//宽 == 需要绘制的数量*放大倍数 + 两个字的宽
bitmap _codeimage = new bitmap(_value.length * ((int)m_magnify + 1), (int)m_height);
graphics _garphics = graphics.fromimage(_codeimage);
_garphics.fillrectangle(brushes.white, new rectangle(0, 0, _codeimage.width, _codeimage.height));
int _lenex = 0;
for (int i = 0; i != _value.length; i++)
{
int _drawwidth = m_magnify + 1;
if (_value[i] == '1')
{
_garphics.fillrectangle(brushes.black, new rectangle(_lenex, 0, _drawwidth, m_height));
}
else
{
_garphics.fillrectangle(brushes.white, new rectangle(_lenex, 0, _drawwidth, m_height));
}
_lenex += _drawwidth;
}
_garphics.dispose();
return _codeimage;
}
/// <summary>
/// 绘制文字,文字宽度大于图片宽度将不显示
/// </summary>
/// <param name="p_codeimage">图形</param>
/// <param name="p_text">文字</param>
private void getviewimage(bitmap p_codeimage, string p_text)
{
if (m_viewfont == null) return;
graphics _graphics = graphics.fromimage(p_codeimage);
sizef _fontsize = _graphics.measurestring(p_text, m_viewfont);
if (_fontsize.width > p_codeimage.width || _fontsize.height > p_codeimage.height - 20)
{
_graphics.dispose();
return;
}
int _starheight = p_codeimage.height - (int)_fontsize.height;
_graphics.fillrectangle(brushes.white, new rectangle(0, _starheight, p_codeimage.width, (int)_fontsize.height));
int _starwidth = (p_codeimage.width - (int)_fontsize.width) / 2;
_graphics.drawstring(p_text, m_viewfont, brushes.black, _starwidth, _starheight);
_graphics.dispose();
}
#endregion
}
}