我会C#点虐 的,不过逻辑应该运核一样,给你看下吧第一步: 创建生成随机字符的方法private string CreateValidate(int count)
为筠连等地区用户提供了全套网页设计制作服务,及筠连网站建设行业解决方案。主营业务为网站设计制作、网站设计、筠连网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
{
//定义验证码中所有的字符
string allchar = "1,2,3,4,5,6,7,8,9,0,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[] allchararray = allchar.Split(',');
//初始化一个随机数
string randomcode = "";
int temp = -1;
/高银/生成一个随机对象
Random rand = new Random();
//根据验证码的位数循环
for (int i = 0; i count; i++)
{
//主要是防止生成相同的验证码
if (temp != -1)
{
//加入时间的刻度
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
//相等的话重新生成
return CreateValidate(count);
}
temp = t;
randomcode += allchararray[t];
}
//在Session中保存随机验证码
Session["Valid"] = randomcode;
//返回生成的随机字符
return randomcode;
}第二步:创建生成图片的方法:private void CreateImageM(string validateCode)
{
//图像的宽度-与验证码的长度成一定比例
int iwidth = (int)(validateCode.Length * 11.5);
//创建一个长20,宽iwidth的图像对象
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
//创建一个新绘图对象
Graphics g = Graphics.FromImage(image);
//绘图用的字体和字号
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
//绘图用的刷子大小
Brush b = new System.Drawing.SolidBrush(Color.White);
//清除背景色,并以深橄榄绿的颜色填充
g.Clear(Color.DarkOliveGreen);
//格式化刷子属性-用指定的刷子、颜色等在指定的范围内画图
g.DrawString(validateCode, f, b, 3, 3);
//创建铅笔对象
Pen blackPen = new Pen(Color.Black, 0);
//创建随机对象
Random rand = new Random();
//随机画线
for (int i = 0; i 5; i++)
{
int y = rand.Next(image.Height);
g.DrawLine(blackPen, 0, y, image.Width, y);
}
//输出绘图
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将图像保存到指定的流
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
//配置输出类型
Response.ContentType = "image/Jpeg";
//输出内容
Response.BinaryWrite(ms.ToArray());
//清空不需要的资源
g.Dispose();
image.Dispose();
}第三步:在page_Load事件中调用CreateImageM(CreateValidate(4));第四步:判断服务器端的验证码是否与用户输入的相同protected void Button1_Click(object sender, EventArgs e)
{
//判断服务器端的验证码是否与用户输入的相同
if (TextBox1.Text == Session["Valid"].ToString())
{
//相同则提示欢迎信息
Response.Write("script language='javascript'alert('欢迎光临')/script");
}
else
{
//不相同则提示错误信息
Response.Write("script language='javascript'alert('对不起,验证码错误')/script");
}
}
建一个YanzhenCard.aspx文件
保留%@ Page Language="VB" AutoEventWireup="false" CodeFile="YanzhenCard.aspx.vb" Inherits="YanzhenCard" %,其余删除
如下是代码:
Imports System.IO
Imports System.Drawing
Partial Class YanzhenCard
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim VNum As String = GenerateRandom(4)
Session("VNum") = VNum
ValidateCode(VNum)
End If
End Sub
Private Sub ValidateCode(ByVal VNum As String)
Dim Gheight As Integer = CType((VNum.Length * 15), Integer)
Dim Img As Bitmap = New Bitmap(Gheight, 20) 'Bitmap是用于处理由像素数据定义的哪梁图像
Dim g As Graphics = Graphics.FromImage(Img) 'Graphics.FromImage 从指定的Image创建新的Graphics
g.DrawString(VNum, New Font("Arial", 12), New SolidBrush(Color.Red), 5, 0) 'g.DrawString()在指定位置并且用指定斗基的Brush和Font对象绘制指定的文本字符串。
Dim ms As MemoryStream = New MemoryStream '创建其支持存储区为内存的流。
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/PNG"
Response.BinaryWrite(ms.ToArray)
Response.Write(VNum)
g.Dispose()
Img.Dispose()
Response.End()
End Sub
Private Shared constant As Char() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "空缓谨X", "Y", "Z"}
Public Shared Function GenerateRandom(ByVal Length As Integer) As String
Dim newRandom As System.Text.StringBuilder = New System.Text.StringBuilder(26)
Dim rd As Random = New Random
Dim i As Integer = 0
While i Length
newRandom.Append(constant(rd.Next(26)))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return newRandom.ToString
End Function
End Class
调用方法:asp:ImageButton ID="Yanzhengma_Card" runat="server" ImageUrl="YanzhenCard.aspx" Height="20px" Width="75px" BorderColor="#6699cc" BorderStyle="Solid" BorderWidth="1px" /
需要写个信春验证码类:
例如
public class ValidCode
public shared function Create() as integer
return Rand(0,100)
end Function
end class
需要在页面里面去调用类的共磨瞎享方法,然后显示验证滑游耐码(你也可以生成图片~~)