十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Dim CtrlRange As Object
成都创新互联是网站建设技术企业,为成都企业提供专业的做网站、成都网站设计,网站设计,网站制作,网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制适合企业的网站。10余年品质,值得信赖!
On Error Resume Next
Do
Err.Clear()
CtrlRange = Ie.Document.Body.DomElement.createControlRange()
CtrlRange.Add(Ie.Document.DomDocument.GetElementById("verifypic"))
CtrlRange.execCommand("Copy") '获取验证码
If Clipboard.ContainsData(DataFormats.Bitmap) Then
PictureBox1.Image = Clipboard.GetData(DataFormats.Bitmap) '在Picture1控件上显示
End If
If Err.Number 0 Then WaitingMS(100) ‘延时
Loop While Err.Number 0
以前找的 vbnet里也可以用
我会C#.net的,不过逻辑应该一样,给你看下吧第一步: 创建生成随机字符的方法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" /