调用说明:str为要生成的代码,ch为条形码的高度,cw为条形码单个字符的宽度 另:当type_code为1时,在条形码下显示代码,否则不显示。public string bar_code(object str, int ch, int cw, int type_code){ string strTmp=str.ToString(); string code=strTmp; strTmp=strTmp.ToLower(); int height=ch; int width=cw;
还有一种方法 在web页面上画出条码的代码 Dim sn As String = Request.QueryString("SN") Dim StockBitMap As Bitmap Dim memStream As New MemoryStream If sn = "" Then Return sn = sn.ToUpper ' sn = Response.ContentType = "image/png" StockBitMap = New Bitmap(130, 60) Dim graph As Graphics = Nothing graph = Graphics.FromImage(StockBitMap) graph.CompositingQuality = CompositingQuality.HighQuality graph.SmoothingMode = SmoothingMode.AntiAlias Dim pfc As PrivateFontCollection = New PrivateFontCollection '载入字体 pfc.AddFontFile(Server.MapPath("font/V100010_.TTF")) Dim fm As New FontFamily("C39HrP48DlTt", pfc) Dim f As New Font(fm, 40) graph.Clear(Color.White) graph.DrawString("*" & sn & "*", f, New SolidBrush(Color.Black), 0, 0) StockBitMap.Save(memStream, ImageFormat.Png) memStream.WriteTo(Response.OutputStream)
另:当type_code为1时,在条形码下显示代码,否则不显示。public string bar_code(object str, int ch, int cw, int type_code){
string strTmp=str.ToString();
string code=strTmp;
strTmp=strTmp.ToLower();
int height=ch;
int width=cw;
strTmp=strTmp.Replace("0", "_|_|__||_||_|");;
strTmp=strTmp.Replace("1", "_||_|__|_|_||");
strTmp=strTmp.Replace("2", "_|_||__|_|_||");
strTmp=strTmp.Replace("3", "_||_||__|_|_|");
strTmp=strTmp.Replace("4", "_|_|__||_|_||");
strTmp=strTmp.Replace("5", "_||_|__||_|_|");
strTmp=strTmp.Replace("7", "_|_|__|_||_||");
strTmp=strTmp.Replace("6", "_|_||__||_|_|");
strTmp=strTmp.Replace("8", "_||_|__|_||_|");
strTmp=strTmp.Replace("9", "_|_||__|_||_|");
strTmp=strTmp.Replace("a", "_||_|_|__|_||");
strTmp=strTmp.Replace("b", "_|_||_|__|_||");
strTmp=strTmp.Replace("c", "_||_||_|__|_|");
strTmp=strTmp.Replace("d", "_|_|_||__|_||");
strTmp=strTmp.Replace("e", "_||_|_||__|_|");
strTmp=strTmp.Replace("f", "_|_||_||__|_|");
strTmp=strTmp.Replace("g", "_|_|_|__||_||");
strTmp=strTmp.Replace("h", "_||_|_|__||_|");
strTmp=strTmp.Replace("i", "_|_||_|__||_|");
strTmp=strTmp.Replace("j", "_|_|_||__||_|");
strTmp=strTmp.Replace("k", "_||_|_|_|__||");
strTmp=strTmp.Replace("l", "_|_||_|_|__||");
strTmp=strTmp.Replace("m", "_||_||_|_|__|");
strTmp=strTmp.Replace("n", "_|_|_||_|__||");
strTmp=strTmp.Replace("o", "_||_|_||_|__|");
strTmp=strTmp.Replace("p", "_|_||_||_|__|");
strTmp=strTmp.Replace("r", "_||_|_|_||__|");
strTmp=strTmp.Replace("q", "_|_|_|_||__||");
strTmp=strTmp.Replace("s", "_|_||_|_||__|");
strTmp=strTmp.Replace("t", "_|_|_||_||__|");
strTmp=strTmp.Replace("u", "_||__|_|_|_||");
strTmp=strTmp.Replace("v", "_|__||_|_|_||");
strTmp=strTmp.Replace("w", "_||__||_|_|_|");
strTmp=strTmp.Replace("x", "_|__|_||_|_||");
strTmp=strTmp.Replace("y", "_||__|_||_|_|");
strTmp=strTmp.Replace("z", "_|__||_||_|_|");
strTmp=strTmp.Replace("-", "_|__|_|_||_||");
strTmp=strTmp.Replace("*", "_|__|_||_||_|");
strTmp=strTmp.Replace("/", "_|__|__|_|__|");
strTmp=strTmp.Replace("%", "_|_|__|__|__|");
strTmp=strTmp.Replace("+", "_|__|_|__|__|");
strTmp=strTmp.Replace(".", "_||__|_|_||_|");
strTmp=strTmp.Replace("_", "<span style='height:"+height+";width:"+width+";background:#FFFFFF;'></span>");
strTmp=strTmp.Replace("|", "<span style='height:"+height+";width:"+width+";background:#000000;'></span>");
if(type_code==1){
return strTmp+"<BR>"+code;
}
else{
return strTmp;
}
}
http://www.codeproject.com/bitmap/barcode2.asp
using System.Drawing.Imaging;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//RndNum是一个自定义函数
string VNum=RndNum(4); //这里的数字4代表显示的是4位的验证字符串!
Session["VNum"]=VNum;
ValidateCode(VNum);
}
//生成图像函数
private void ValidateCode(string VNum)
{
int Gheight=(int)(VNum.Length * 11.5);
//gheight为图片宽度,根据字符长度自动更改图片宽度
System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
Graphics g = Graphics.FromImage(Img);
g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3);
//在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
System.IO.MemoryStream ms=new System.IO.MemoryStream();
Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType="image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}//生成随机数函数中从Vchar数组中随机抽取
//字母区分大小写
public string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z" ;
string[] VcArray = Vchar.Split(',') ;
string VNum = "" ;//由于字符串很短,就不用StringBuilder了
int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数 //采用一个简单的算法以保证生成随机数的不同
Random rand =new Random();
for ( int i = 1 ; i < VcodeNum+1 ; i++ )
{
if ( temp != -1)
{
rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
}
//int t = rand.Next(35) ;
int t=rand.Next(35);
if (temp != -1 && temp == t)
{
return RndNum( VcodeNum );
}
temp = t ;
VNum += VcArray[t]; }
return VNum ;
}
-----------------------------------end--------------------------------------------下面是Login.Aspx在在调用提交按钮的响应事件中
public void doit(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
string VNum;
VNum=Session["VNum"].ToString();
Session.Abandon();
ViewState["VNum"]=VNum;
if(this.Vcode.Text==ViewState["VNum"].ToString())
{ Hover.Manage.CheckLogin obj=new Hover.Manage.CheckLogin();
string name=username.Text;
string password=FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Text.ToString(),"md5");
if(!obj.checklogin(name,password))
{
Response.Redirect("../Error.aspx?action=Errorlogin");
Response.End();
return;
}
else
{
Session.Add("adminname",name);
Session.Add("adminpass",password);
Response.Redirect("Default.aspx");
Response.End();
return;
}
}
else
{
Response.Write("<script>alert(\"请输入正确的附加码!\");</script>");
}
}
}
如果提交过来的验证字符串正确就验证是否是合法用户!
否则弹出提示返回登陆页!你也可以简化此页!
if(this.Vcode.Text==ViewState["VNum"].ToString())
{
Response.Write("验证码正确");
}
else
{
Response.Write("验证码错误!");
}
Dim StockBitMap As Bitmap
Dim memStream As New MemoryStream
If sn = "" Then Return
sn = sn.ToUpper
' sn =
Response.ContentType = "image/png"
StockBitMap = New Bitmap(130, 60)
Dim graph As Graphics = Nothing
graph = Graphics.FromImage(StockBitMap)
graph.CompositingQuality = CompositingQuality.HighQuality
graph.SmoothingMode = SmoothingMode.AntiAlias
Dim pfc As PrivateFontCollection = New PrivateFontCollection
'载入字体 pfc.AddFontFile(Server.MapPath("font/V100010_.TTF"))
Dim fm As New FontFamily("C39HrP48DlTt", pfc)
Dim f As New Font(fm, 40)
graph.Clear(Color.White)
graph.DrawString("*" & sn & "*", f, New SolidBrush(Color.Black), 0, 0)
StockBitMap.Save(memStream, ImageFormat.Png)
memStream.WriteTo(Response.OutputStream)
好像不支持b/s