1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11
using System.Drawing;12
using System.Drawing.Imaging;13

14
public partial class Build39Code : System.Web.UI.Page15


{16
protected void Page_Load(object sender, EventArgs e)17

{18
string s = Request.QueryString["target"];19
if (string.IsNullOrEmpty(s)) return;20
s = s.ToUpper();21
if (s.StartsWith("*") && s.EndsWith("*"))22

{ }23

else
{24
s = string.Format("*{0}*", s);25
}26
CODE39DrawImageBord dr = new CODE39DrawImageBord(s);27
dr.Draw(Response.OutputStream); 28

29
}30
}31

32

33

34
public abstract class DrawImageBord35


{36
protected virtual string BordRuleName37

{38

get
{ return string.Empty; }39
}40

41
protected virtual System.Collections.Hashtable Roles42

{43

get
{ return new System.Collections.Hashtable(); }44
}45

46
string drawString;47
int width = 0; //画布的宽度(可计算) 48
int height = 36;//1CM 49
int unitWidth = 1; // 50

51
int currentLocation = 0;52

53
public DrawImageBord(string s)54

{55
drawString = s;56
}57

58
public virtual void Draw(System.IO.Stream target)59

{60
if (width == 0)61

{62
width = drawString.Length * 30;63
}64
Bitmap bm = new Bitmap(width, height);65
Graphics g = Graphics.FromImage(bm);66
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;67
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;68

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

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

74
for (int i = 0; i < drawString.Length; i++)75

{76
this.DrawString(drawString[i].ToString(), g);77
}78
// 79
bm.Save(target, ImageFormat.Jpeg);80
}81

82
protected virtual void DrawString(string s, Graphics g)83

{84
System.Collections.Hashtable hash = this.Roles;85
object o = hash[s];86
if (o == null) return;87
char[] chars = o.ToString().ToCharArray();88
if (chars.Length > 9) return;89

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

93
for (int i = 0; i < 5; i++)94

{95
//画第一个 0 黑条 96
if (chars[i] == '0')97

{98
Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);99
g.FillRectangle(blackBrush, re1);100
currentLocation += unitWidth;101
}102
else103

{104
Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);105
g.FillRectangle(blackBrush, re1);106
currentLocation += 3 * unitWidth;107
}108

109
//画第6个 5 白条 110
if ((i + 5) < 9)111

{112
if (chars[i + 5] == '0')113

{114
Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);115
g.FillRectangle(witeBrush, re1);116
currentLocation += unitWidth;117
}118
else119

{120
Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);121
g.FillRectangle(witeBrush, re1);122
currentLocation += 3 * unitWidth;123
}124
}125
}126

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

131
}132
}133

134
public class CODE39DrawImageBord : DrawImageBord135


{136
private System.Collections.Hashtable hash = new System.Collections.Hashtable();137
protected override string BordRuleName138

{139

get
{ return "CODE39 "; }140
}141
public CODE39DrawImageBord(string s)142
: base(s)143

{144

145
}146

147
protected override System.Collections.Hashtable Roles148

{149
get150

{151
if (hash.Count > 0) return hash;152

153
hash.Add("0", "001100100");154
hash.Add("1", "100010100");155
hash.Add("2", "010010100");156
hash.Add("3", "110000100");157
hash.Add("4", "001010100");158
hash.Add("5", "101000100");159
hash.Add("6", "011000100");160
hash.Add("7", "000110100");161

162
hash.Add("8", "100100100");163
hash.Add("9", "010100100");164
hash.Add("A", "100010010");165
hash.Add("B", "010010010");166
hash.Add("C", "110000010");167
hash.Add("D", "001010010");168
hash.Add("E", "101000010");169

170
hash.Add("F", "011000010");171
hash.Add("G", "000110010");172
hash.Add("H", "100100010");173
hash.Add("I", "010100010");174
hash.Add("J", "001100010");175
hash.Add("K", "100010001");176
hash.Add("L", "010010001");177

178
hash.Add("M", "110000001");179
hash.Add("N", "001010001");180
hash.Add("O", "101000001");181
hash.Add("P", "011000001");182
hash.Add("Q", "000110001");183
hash.Add("R", "100100001");184
hash.Add("S", "010100001");185

186

187
hash.Add("T", "001100001");188
hash.Add("U", "100011000");189
hash.Add("V", "010011000");190
hash.Add("W", "110001000");191
hash.Add("X", "001011000");192
hash.Add("Y", "101001000");193
hash.Add("Z", "011001000");194
195
hash.Add("-", "000111000");196
hash.Add("%", "100101000");197
hash.Add("$", "010101000");198
hash.Add("*", "001101000");199

200
return hash;201

202
}203
}204
} 205

要加字,就用DrawString
至于你的要写个条码值
用gdi再写个字就好了类似下面
- C# code
- Bitmap bm = new Bitmap(width, height + 24 ); Graphics g = Graphics.FromImage(bm); .... for (int i = 0; i < drawString.Length; i++) { this.DrawString(drawString[i].ToString(), g); } //字体 Font drawFont = new Font("Arial", 16); //颜色 SolidBrush drawBrush = new SolidBrush(Color.Black); //字体所在矩阵 RectangleF drawRect = new RectangleF(0F, (float)this.height , this.width, (float)24 ); //写字 g.DrawString(this.drawString, drawFont, drawBrush, drawRect);
浙公网安备 33010602011771号