Webfrom --图片验证码
生活随笔
收集整理的這篇文章主要介紹了
Webfrom --图片验证码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(一) 添加一個(gè)一般處理程序,
<%@ WebHandler Language="C#" Class="show" %>using System; using System.Web; using System.Drawing;//引用 using System.Web.SessionState;//IRequiresSessionState的命名空間public class show : IHttpHandler, IRequiresSessionState {public void ProcessRequest(HttpContext context){context.Response.ContentType = "image/jpeg";//要輸出的類(lèi)型Bitmap img = new Bitmap(50, 20);//造空白圖Graphics gr = Graphics.FromImage(img);//往哪個(gè)圖上去繪制Font font = new Font("宋體", 12, FontStyle.Bold);//設(shè)置字體SolidBrush brush = new SolidBrush(Color.White);//設(shè)置刷子gr.FillRectangle(brush, 0, 0, 50, 20);//刷子繪制的形狀brush.Color = Color.Red;//顏色string s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";string str = "";Random rand = new Random();//初始化隨機(jī)數(shù)for (int i = 0; i < 4; i++){int start = rand.Next(62); //生成一個(gè)隨機(jī)的起始位置str += s.Substring(start, 1).ToString();}//需要繼承接口context.Session["yanzheng"] = str;gr.DrawString(str, font, brush, 0, 0);//繪制完了圖片了//將圖片保存,通過(guò)response響應(yīng)流保存 img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);}public bool IsReusable{get{return false;}}}(二)制作一個(gè)主頁(yè)面 Default
添加一個(gè)imagebutton , textbox ,button
不要忘記在imageUrl中添加寫(xiě)的圖片的方法。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e){}protected void Button1_Click(object sender, EventArgs e){string txtyanzheng = TextBox1.Text;if (string.IsNullOrWhiteSpace(txtyanzheng)){}else{string yanzheng = Session["yanzheng"].ToString(); //用Session傳值接收過(guò)來(lái)if (txtyanzheng == yanzheng){Response.Write("<script>alert('注冊(cè)成功')</script>");}else{Response.Write("<script>alert('驗(yàn)證碼不正確')</script>");}}}}效果圖:
?
轉(zhuǎn)載于:https://www.cnblogs.com/w-wz/p/4661432.html
總結(jié)
以上是生活随笔為你收集整理的Webfrom --图片验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: hdu 4035 可能性DP 成都网
- 下一篇: 从HTML5移动应用现状谈发展趋势