散分,发布一个生成CODE39条形码的代码

zyug 2007-08-20 09:57:09

长期做WEB对GDI不熟,画的不对的还请多多指教


public abstract class DrawImageBord
{
protected virtual string BordRuleName
{
get { return string.Empty; }
}

protected virtual System.Collections.Hashtable Roles
{
get { return new System.Collections.Hashtable(); }
}

string drawString;
int width = 800; //画布的宽度(可计算)
int height = 36;//1CM
int unitWidth = 1; //

int currentLocation = 0;

public DrawImageBord(string s)
{
drawString = s;
}

public virtual void Draw(System.IO.Stream target)
{
Bitmap bm = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bm);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

//画布和边的设定
g.Clear(Color.White);

g.DrawRectangle(Pens.White, 0, 0, width, height);

for (int i = 0; i < drawString.Length; i++)
{
this.DrawString(drawString[i].ToString(), g);
}
//
bm.Save(target, ImageFormat.Jpeg);
}

protected virtual void DrawString(string s, Graphics g)
{
System.Collections.Hashtable hash = this.Roles;
object o = hash[s];
if (o == null) return;
char[] chars = o.ToString().ToCharArray();
if (chars.Length > 9) return;

SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush witeBrush = new SolidBrush(Color.White);

for (int i = 0; i < 5; i++)
{
//画第一个 0 黑条
if (chars[i] == '0')
{
Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
g.FillRectangle(blackBrush, re1);
currentLocation += unitWidth;
}
else
{
Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
g.FillRectangle(blackBrush, re1);
currentLocation += 3 * unitWidth;
}

//画第6个 5 白条
if ((i + 5) < 9)
{
if (chars[i + 5] == '0')
{
Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
g.FillRectangle(witeBrush, re1);
currentLocation += unitWidth;
}
else
{
Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
g.FillRectangle(witeBrush, re1);
currentLocation += 3 * unitWidth;
}
}
}

Rectangle re2 = new Rectangle(currentLocation, 0, unitWidth, height);
g.FillRectangle(witeBrush, re2);
currentLocation += unitWidth;

}
}

public class CODE39DrawImageBord : DrawImageBord
{
private System.Collections.Hashtable hash = new System.Collections.Hashtable();
protected override string BordRuleName
{
get { return "CODE39"; }
}
public CODE39DrawImageBord(string s) : base(s)
{

}

protected override System.Collections.Hashtable Roles
{
get {
if (hash.Count > 0) return hash;
hash.Add("0", "001100100");
hash.Add("1", "100010100");
hash.Add("2", "010010100");
hash.Add("3", "110000100");
hash.Add("4", "001010100");
hash.Add("5", "101000100");
hash.Add("6", "011000100");
hash.Add("7", "000110100");

hash.Add("8", "100100100");
hash.Add("9", "010100100");
hash.Add("A", "100010010");
hash.Add("B", "010010010");
hash.Add("C", "110000010");
hash.Add("D", "001010010");
hash.Add("E", "101000010");

hash.Add("F", "011000010");
hash.Add("G", "000110010");
hash.Add("H", "100100010");
hash.Add("I", "010100010");
hash.Add("J", "001100010");
hash.Add("K", "100010001");
hash.Add("L", "010010001");

hash.Add("M", "110000001");
hash.Add("N", "001010001");
hash.Add("O", "101000001");
hash.Add("P", "011000001");
hash.Add("Q", "000110001");
hash.Add("R", "100100001");
hash.Add("S", "010100001");


hash.Add("T", "001100001");
hash.Add("U", "100011000");
hash.Add("V", "010011000");
hash.Add("W", "110001000");
hash.Add("X", "001011000");
hash.Add("Y", "101001000");
hash.Add("Z", "011001000");


hash.Add("-", "000111000");
hash.Add("%", "100101000");
hash.Add("$", "010101000");
hash.Add("*", "001101000");

return hash;

}
}
}

//调用
protected void Page_Load(object sender, EventArgs e)
{

CODE39DrawImageBord dr = new CODE39DrawImageBord("*3949178*");
dr.Draw(Response.OutputStream);
//然后print用扫描器读.
}

// 39码前后要画*号,如果不画* 号,我这边的扫描器不读.....
...全文
1420 52 打赏 收藏 转发到动态 举报
写回复
用AI写文章
52 条回复
切换为时间正序
请发表友善的回复…
发表回复
yezi0221 2009-01-22
  • 打赏
  • 举报
回复
谢谢.太谢谢了.
叶子 2007-08-20
  • 打赏
  • 举报
回复
学习了!受教!
mrshelly 2007-08-20
  • 打赏
  • 举报
回复
服务端字体生成图片......
蝶恋花雨 2007-08-20
  • 打赏
  • 举报
回复
学习下收藏了
Gangzai1983 2007-08-20
  • 打赏
  • 举报
回复
Mark
pueler 2007-08-20
  • 打赏
  • 举报
回复
up
zyug 2007-08-20
  • 打赏
  • 举报
回复
区别主要在于线条的规则不一样
39码,由一个9位的线条来表示一个字符
其中,粗线条只有三条
我们把粗的用1来表示,细的用0来表示
1 2 3 4 5
6 7 8 9

共有9位,12345是黑条,6789是白条

如果是 0 : 001100100

12345 为黑条, 6789为白条

其中 34为黑粗条 7组粗白条,

128码我没有查..估计就是编码不一样
conannb 2007-08-20
  • 打赏
  • 举报
回复
up
dcxa_ni 2007-08-20
  • 打赏
  • 举报
回复
CODE39 和 code128 什么差别
满衣兄 2007-08-20
  • 打赏
  • 举报
回复
up
wdzr_826 2007-08-20
  • 打赏
  • 举报
回复
呵呵,以前作了个code128的
loverdotnet 2007-08-20
  • 打赏
  • 举报
回复
mark
ldw701 2007-08-20
  • 打赏
  • 举报
回复
实用,不过我还没有用过。
milo4210 2007-08-20
  • 打赏
  • 举报
回复
JF
wszhoho 2007-08-20
  • 打赏
  • 举报
回复
实用,不过我还没有用过。
symbol441 2007-08-20
  • 打赏
  • 举报
回复
up
hu0516 2007-08-20
  • 打赏
  • 举报
回复
我参与的帖子
zhoucaifu 2007-08-20
  • 打赏
  • 举报
回复
andy888666 2007-08-20
  • 打赏
  • 举报
回复
作记号,以备日后使用
xinshijiuu 2007-08-20
  • 打赏
  • 举报
回复
自定义控件?
加载更多回复(32)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧