注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

午夜阳光

 
 
 

日志

 
 

验证码  

2015-01-09 17:13:20|  分类: Asp.net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

生成验证码的前台<script>
    function changeImage() {
        document.getElementById("imgRandom").src = document.getElementById("imgRandom").src+'?';
    }
</script>

后台

public partial class RandomImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateCheckCodeImage(CreateCheckCode());
    }
    /// <summary>
    /// 创建验证码
    /// </summary>
    /// <returns></returns>
    private string CreateCheckCode()
    {
        //验证码长度
        int codeLength = 4;
        int number;
        string randomCode = string.Empty;
        Random r = new Random();
        for (int i = 0; i < codeLength; i++)
        {
            number = r.Next();
            //字符从0-9 A-Z中随机产生对应ascll码分别为48-57 65-90
            number = number % 36;
            if (number < 10)
            {
                number += 48;
            }
            else
            {
                number += 55;
            }
            randomCode += ((char)number).ToString();
        }
        //将 验证码保存到Cookies中
        Response.Cookies.Add(new HttpCookie("CheckCode", randomCode));
        return randomCode;
    }
    private void CreateCheckCodeImage(string checkCode)
    {
        //验证码为空直接return
        if (checkCode == null || checkCode.Trim() == string.Empty)
            return;
        //根据验证码长度确定输入图片的宽度
        int imgWidth = (int)Math.Ceiling(checkCode.Length * 15m);
        int imgHeight = 20;
        // 创建图片
        Bitmap image = new Bitmap(imgWidth,imgHeight);
        Graphics graph = Graphics.FromImage(image);
        try
        {
            Random rd = new Random();
            //清空图片背景
            graph.Clear(Color.White);
            // 画10条线
            for (int i = 0; i < 10; i++)
            {
                int x1 = rd.Next(image.Width);
                int x2 = rd.Next(image.Width);
                int y1 = rd.Next(image.Height);
                int y2 = rd.Next(image.Height);
                //选择线的颜色
                graph.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);//创建了一支画笔画线
            }
            //画小点
            for (int i = 0; i < 50; i++)
            {
                int x = rd.Next(image.Width);
                int y = rd.Next(image.Height);
                image.SetPixel(x, y, Color.FromArgb(rd.Next()));
            }
            //画边框
            graph.DrawRectangle(new Pen(Color.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);
            //定义字体
            Font f = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
            graph.DrawString(checkCode, f, brush, 2, 2);
            using (MemoryStream ms = new MemoryStream())
            {
                //将图片保存于内存中
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                Response.ClearContent();
                Response.ContentType = "image/Png";
                Response.BinaryWrite(ms.ToArray());
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            graph.Dispose();
            image.Dispose();
        }
    }
}

 

调用:

<table width="100%" border="0">
                                                            <tr>
                                                                <td style="width: 65px">
                                                                    <asp:TextBox ID="txtYan" runat="server" autocomplete="off" Width="60px" CssClass="easyui-textbox"
                                                                        Height="32px"></asp:TextBox>
                                                                </td>
                                                                <td align="left" style="width: 65px">
                                                 <img id="imgRandom" src="Register/RandomImage.aspx" style="padding-top: 3px" border="0"
                                                                        alt="验证码" />
                                                                </td>
                                                                <td align="left">
                                                                    <a href="javascript:changeImage();">看不清?</a>
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td colspan="3">
                                                                    <asp:Label ID="lblMse" runat="server" ForeColor="Red" Visible="false"></asp:Label>
                                                                </td>
                                                            </tr>
                                                        </table>

  评论这张
 
阅读(70)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017