当前位置:编程学习 > C#/ASP.NET >>

生成图象验证码函数

答案:     ''生成图象验证码函数
  
  
  Sub ValidateCode(ByVal VNum As String)
  
  
  Dim Img As System.Drawing.Bitmap
  
  
  Dim g As Graphics
  
  
  Dim ms As System.IO.MemoryStream
  
  
  ''gheight为图片宽度,根据字符长度自动更改图片宽度
  
  
  Dim gheight As Integer = Int(Len(VNum) * 11.5)
  
  
  ''创建一个宽度已定,高度为20的图像
  
  
  Img = New Bitmap(gheight, 20)
  
  
  g = Graphics.FromImage(Img)
  
  
  ''在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
  
  
  g.DrawString(VNum, (New Font("宋体", 12)), (New SolidBrush(Color.Blue)), 3, 3)
  
  
  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()
  
  
  End Sub
  
  
  
  

上一个:在ASP.NET 2.0中使用页面导航控件(1)
下一个:利用ASP.NET 2.0创建自定义Web控件(6)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,