日期:2014-05-18  浏览次数:20484 次

急求条形码代码,满意即给全分
急~~~~~~~~~~~~~~




------解决方案--------------------
呵呵,你运气好。
最近在CSDN上挖到的...


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;