ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)

VB.net捕获整个网页并保存成图像 VB.net捕获整个网页并保存成图像有软件 Private Sub GetImage() If WebBrowser1.Document Is Nothing Then Return End If Try Dim scrollWidth As Integer Dim scrollHeight As Integer scrollHeight = WebBrowser1.Document.Body.ScrollRectangle.Height scrollWidth = WebBrowser1.Document.Body.ScrollRectangle.Width WebBrowser1.Size = New Size(scrollWidth, scrollHeight) Dim bm As New Bitmap(scrollWidth, scrollHeight) WebBrowser1.DrawToBitmap(bm, New Rectangle(0, 0, bm.Width, bm.Height)) Dim SaveAsName As String SaveAsName = Regex.Replace(textWebURL.Text, "(\\|\/|\:|\*|\?|\""|\<|\>|\|)?", "") bm.Save(SaveAsName & ".png", System.Drawing.Imaging.ImageFormat.Png) bm.Dispose() Catch ex As Exception MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error) Finally ' End Try buttonCapture.Enabled = True End Sub 立即下载
 
学习整理了一下
(一).功能
 
      用HttpHandler实现图片验证码
     
 
(二).代码如下
 
 1. 处理程序文件 ValidateImageHandler.ashx代码如下
 
 1  <% @ WebHandler Language = " C# "  Class = " ValidateImageHandler "   %>
 2 
 3  using  System;
 4  using  System.Web;
 5  using  System.Web.SessionState;
 6  using  System.Drawing;
 7  using  System.Drawing.Imaging;
 8  using  System.Text;
 9 
10  ///  
11 /// ValidateImageHandler 生成网站验证码功能
12 /// 

13  public   class  ValidateImageHandler : IHttpHandler, IRequiresSessionState
14  {
15       int  intLength  =   5 ;                // 长度
16       string  strIdentify  =   " Identify " ;  // 随机字串存储键值,以便存储到Session中
17       public  ValidateImageHandler()
18      {        
19      }
20 
21       ///  
22     ///  生成验证图片核心代码
23     /// 

24       ///  
25       public   void  ProcessRequest(HttpContext hc)
26      {
27           // 设置输出流图片格式
28          hc.Response.ContentType  =   " image/gif " ;
29          
30          Bitmap b  =   new  Bitmap( 200 ,  60 );
31          Graphics g  =  Graphics.FromImage(b);
32          g.FillRectangle( new  SolidBrush(Color.YellowGreen),  0 ,  0 ,  200 ,  60 );
33          Font font  =   new  Font(FontFamily.GenericSerif,  48 , FontStyle.Bold, GraphicsUnit.Pixel);
34          Random r  =   new  Random();
35 
36           // 合法随机显示字符列表
37           string  strLetters  =   " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " ;
38          StringBuilder s  =   new  StringBuilder();
39          
40           // 将随机生成的字符串绘制到图片上
41           for  ( int  i  =   0 ; i  <  intLength; i ++ )
42          {
43              s.Append(strLetters.Substring(r.Next( 0 , strLetters.Length  -   1 ),  1 ));
44              g.DrawString(s[s.Length  -   1 ].ToString(), font,  new  SolidBrush(Color.Blue), i  *   38 , r.Next( 0 ,  15 ));
45          }
46 
47           // 生成干扰线条
48          Pen pen  =   new  Pen( new  SolidBrush(Color.Blue),  2 );
49           for  ( int  i  =   0 ; i  <   10 ; i ++ )
50          {
51              g.DrawLine(pen,  new  Point(r.Next( 0 ,  199 ), r.Next( 0 ,  59 )),  new  Point(r.Next( 0 ,  199 ), r.Next( 0 ,  59 )));
52          }
53          b.Save(hc.Response.OutputStream, ImageFormat.Gif);
54          hc.Session[strIdentify]  =  s.ToString();  // 先保存在Session中,验证与用户输入是否一致
55          hc.Response.End();
56     
57      }
58      
59       ///  
60     /// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
61     /// 

62       public   bool  IsReusable
63      {
64           get
65          {
66               return   true ;
67          }
68      }
69  }
70 

 2. 前台页面代码

 1  < asp:Login ID = " Login1 "  runat = " server "  BackColor = " #EFF3FB "  BorderColor = " #B5C7DE "  BorderPadding = " 4 "  BorderStyle = " Solid "  BorderWidth = " 1px "  Font - Names = " Verdana "  Font - Size = " 0.8em "  ForeColor = " #333333 "  OnAuthenticate = " Login1_Authenticate " >
 2               < TitleTextStyle BackColor = " #507CD1 "  Font - Bold = " True "  Font - Size = " 0.9em "  ForeColor = " White "   />
 3               < InstructionTextStyle Font - Italic = " True "  ForeColor = " Black "   />
 4               < TextBoxStyle Font - Size = " 0.8em "   />
 5               < LoginButtonStyle BackColor = " White "  BorderColor = " #507CD1 "  BorderStyle = " Solid "  BorderWidth = " 1px "
 6                  Font - Names = " Verdana "  Font - Size = " 0.8em "  ForeColor = " #284E98 "   />
 7               < LayoutTemplate >
 8                   < table border = " 0 "  cellpadding = " 4 "  cellspacing = " 0 "  style = " border-collapse: collapse " >
 9                       < tr >
10                           < td style = " width: 292px " >
11                               < table border = " 0 "  cellpadding = " 0 " >
12                                   < tr >
13                                       < td align = " center "  colspan = " 2 "  style = " font-weight: bold; font-size: 0.9em; color: white;
14                                          background - color: #507cd1 " >
15                                          登录 td>
16                                  tr>
17                                 <tr>
18                                     <td align="left" style="width: 84px; height: 31px;">
19                                         <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名: asp:Label> td>
20                                     <td style="height: 31px; width: 215px;">
21                                         <asp:TextBox ID="UserName" runat="server" Font-Size="0.8em" Width="113px"> asp:TextBox>
22                                         <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
23                                             ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">* asp:RequiredFieldValidator>
24                                      td>
25                                  tr>
26                                 <tr>
27                                     <td align="left" style="width: 84px">
28                                         <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码: asp:Label> td>
29                                     <td style="width: 215px">
30                                         <asp:TextBox ID="Password" runat="server" Font-Size="0.8em" TextMode="Password"> asp:TextBox>
31                                         <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
32                                             ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">* asp:RequiredFieldValidator>
33                                      td>
34                                  tr>
35                                 <tr>
36                                     <td style="width: 84px; height: 4px;" align="left">
37                                         验证码: td>
38                                     <td valign="middle" style="height: 31px; width: 215px;" align="left">
39                                         <asp:TextBox ID="TextBox1" runat="server" Font-Size="0.8em" TextMode="Password"> asp:TextBox>&nbsp;
40                                         
                                 
                                        td>
41                                  tr>
42                                 <tr>
43                                     <td align="left" colspan="2" style="color: red">
44                                         <asp:CheckBox ID="RememberMe" runat="server" Text="下次记住我。" />&nbsp; td>
45                                  tr>
46                                 <tr>
47                                     <td align="right" colspan="2">
48                                         <asp:Button ID="LoginButton" runat="server" BackColor="White" BorderColor="#507CD1"
49                                             BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
50                                             Font-Size="0.8em" ForeColor="#284E98" Text="登录" ValidationGroup="Login1" />
51                                      td>
52                                  tr>
53                              table>
54                          td>
55                      tr>
56                  table>
57              LayoutTemplate>
58         
59          asp:Login>
60 

  3.这里因为使用的是默认 *.asah处理文件类型,在machine.config文件中已经有此类型的默认注册,

 因为这里不需要注册
 
1  < httpHandlers >
2     < add verb = " * "  path = " *.asah "  type = " System.Web.UI.SimpleHandlerFactory "   />
3  httpHandlers>
4 

 注意:

1.再注册一下也不会出错,会覆盖machine.config文件配置

2.如果在同一个配置文件中注册多次,默认后者也会覆盖前者.

3.如果其它格式(系统默认没有注册)的,务必要在Web.config文件中注册一下.

 
(三).示例代码下载
   


Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1475140


asp.net HttpHandler实现图片防盗链 Step.1:创建文件 CustomHandler.cs,代码如下: 代码如下: using System; using System.Web; namespace CustomHandler{ public class JpgHandler : IHttpHandler{ public void ProcessRequest(HttpContext context){ // 获取文件服务器端物理路径 string FileName = context.Server.MapPath(context.Request.FilePath); // 如果UrlReferrer为空,则显示一张默认的禁 立即下载

相关推荐

论文浅读之PIP-Net: Patch-Based Intuitive Prototypes for Interpretable Image Classification

这篇文章的主要贡献包括:1 提出了一种新型的可解释图像分类模型PIP-Net,它基于原型部分学习可解释的原型,实现内置解释性。2 PIP-Net采用自监督学习的新正则化方法,学习原型之间的相似性,使得 latent 空间中的相似性能更好地与人类视觉感知相一致,从而减小了所谓的语义差距。3 PIP-Net可解释为一个稀疏的打分表,它可以检测图像是否属于任何类,或是否属于多个类。它也可以对OOD数据进行分类并说“我没有看到过这个”。

qq_49370210的博客 1791

ASP.NET 2.0 HttpHandler实现生成图片验证码(转)

1. 处理程序文件 ValidateImageHandler.ashx代码如下 @ WebHandler Language="C#" Class="ValidateImageHandler" %> using System; using System.We

lchg66的专栏 623

Linux系统下Materials Studio 2020安装避坑指南:从依赖包到许可证配置全流程

本文提供了一份在Linux系统上安装Materials Studio 2020的深度避坑指南。详细解析了从系统依赖包检查、安装流程、许可证配置到Gateway服务部署的全过程,并分享了与Slurm作业调度系统集成的实战经验,旨在帮助用户高效、稳定地完成部署。

weixin_29313547的博客 387

使用ASP.NET 2.0 HttpHandler实现生成图片验证码的问题

最近做登录、注册界面,用到验证码,找了些资料。在http://blog.csdn.net/chengking/archive/2007/01/09/1475140.aspx这篇文章中,给了使用ASP.NET 2.0 HttpHandler实现生成图片验证码的例子,还是不明白如何判断验证码是否正确,ValidateImageHandler.ashx@ WebHandler Langu

wujiancumt的专栏 880

别再只怪贴片厂了!从MLCC内部裂纹,看懂物料质量与工艺应力的博弈

本文深入分析了MLCC内部裂纹的失效原因,提供了从裂纹形态学到生产全流程的失效风险地图的完整分析框架。通过实战案例和检测技术组合,帮助工程师精准锁定MLCC失效背后的真凶,优化物料质量和工艺应力管理。

weixin_33122947的博客 293

优快云 付费专栏连载第 3 篇|Cadence Allegro 8 层板布局实战教程:RK3568 硬件模块化分区布局,DDR4 内存约束区域设置、布局禁忌避坑指南

本文是 RK3568 8 层板高速 PCB 设计实战专栏的第 3 篇,聚焦 DDR4 布局阶段的核心方法论与 Allegro 实操。文章从布局前 6 项前置检查清单入手,系统讲解模块化分区布局的五大黄金原则,并给出 160mm×100mm 样板的 5 大功能分区实战案例。随后以图文步骤演示如何在 Allegro 中创建 ROOM_DDR4 与 ROOM_CPU 约束区域,并强调 Hard Room 硬约束的强制校验作用。

weixin_44520581的博客 1733

HttpHandler 创建图片验证码

1 打开Vs2005,“添加新项”,“一般处理程序”。新建文件后,VS会自动在文件中添加如下的代码: 2:创建以下程序 <%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using System.Drawing.Imaging; using System.Dra...

weixin_34150503的博客 181

YOLO11优化:特征融合 | 金字塔稀疏 Transformer(PST)——一个轻量级、即插即用的模块 | 25年7月最新成果

我们提出金字塔稀疏 Transformer(PST)——一个轻量级、即插即用的模块,通过由粗到细的 token 选择机制和共享注意力参数,在大幅降低计算量的同时保留空间细节。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 1062
上一篇: 如何才能成为信息顾问
下一篇: 书讯 -- SQL Server 2005 Practical Troubleshooting The Database Engine
itkbase
博客等级 码龄19年 113粉丝 4646原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值