c# 生成验证码

C#实现登录验证码  最近在开发中遇到要实现登录验证码的功能,在网上借鉴了一下并加以扩展,供大家分享.代码如下:///首先新建一个ValidateCode.ashx文件.using System;using System.Data;using System.Web;using System.Collections;using System.Web.Services;using System.Web.Ser 阅读详情

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;

public partial class checkcode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string checkCode = CreateRandomCode(4);
        Session["CheckCode"] = checkCode;
        CreateImage(checkCode);

    }
    private string CreateRandomCode(int codeCount)
    {
        string allChar = "0,1,2,3,4,5,6,7,8,9,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 < codeCount; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(35);
            if (temp == t)
            {
                return CreateRandomCode(codeCount);
            }
            temp = t;
            randomCode += allCharArray[t];
        }
        return randomCode;
    }
    private void CreateImage(string checkCode)
    {
        int iwidth = (int)(checkCode.Length * 11.5);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
        Graphics g = Graphics.FromImage(image);
        Font f = new System.Drawing.Font("Arial", 8, System.Drawing.FontStyle.Regular);
        Brush b = new System.Drawing.SolidBrush(Color.White);
        //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
        g.Clear(Color.Blue);
        g.DrawString(checkCode, 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();
    }

C# 生成图片验证码web、winds / 将图片转换为Base64字符串。// 生成随机字符串。注意:上述代码中使用了 System.Drawing 命名空间,因此需要确保安装了相应的依赖(如 GDI+)。// 生成验证码图片。这样就可以在 C#生成图片验证码了。 阅读详情

相关推荐

C#实现验证码噪点生成教程源码

本文还有配套的精品资源,点击获取 简介:验证码是网络应用中重要的安全措施,使用C#和GDI+库可生成包含文本和噪点的复杂验证码图片。本文介绍C#中创建验证码的详细步骤,包括随机字符串生成、背景创建、文本绘制、噪声添加、图像扭曲模糊处理以及保存和显示验证码图片。提供的源码包" C#绘制验证码噪点源码_6.rar "可能包含实现这些功能的关键代码和函数。开发者可以...

weixin_35734408的博客 1604

C#生成简单验证码

我们平时无论是网站登录还是注册,都会频繁的遇到各式各样的验证码 ,其实生成验证码对于C#来说非常简单。 下面就是我学习生成验证码的简单实例。 封装的辅助类代码,如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Drawing.Dra...

weixin_30725315的博客 371

C#生成验证码(纯数字

新建一个验证码生成类,名为ValidateCodeHelper.cs using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using Syste

fly043488的专栏 6398

C#生成验证码

public static bool GenRad(string strRadnum,System.IO.Stream oOutStream,int nWidth,int nHeight)  {   System.Drawing.Bitmap oImage;     try   {    oImage=new System.Drawing.Bitmap(nWidth,nHeight);      

快乐鸟的专栏 1361

C#简单数字验证码解析

这几天都在研究验证码解析,写了一个DEMO,对于纯数字生成位置变化频率较低的图片识别效果还算满意,准确率在80%以上,更高级别的验证码还有待研究。 CSharp简单数字验证码解析.rar 具体实现思路:以4位数字验证码为例 1、人工将验证码的4位数字每位对应的代码存入数据库中,每位存入0-9对应的代码,每个数可以多存这样可以提高识别率; 2、获取验证码以后,对其进行去背景、灰度处理、去噪点...

weixin_34292287的博客 230

C# 生成图形验证码

图形验证码是一个网络安全技术手段,防止恶意程序自动攻击执行。图形验证码的呈现形式有很多种,这里我们将介绍最基本的生成方式,字母及数字的呈现方式。

初九之潜龙勿用 2024

C#验证码生成

C#验证码生成 前言:验证码的重要性,验证码作为一个标识,通过判断他的正确性决定要不要用户去执行相关的操作。验证码生成方法也就那几种,这里主要讲一些比较简单的生成方法。 思路: 1、创建一个ValidCodeUtils静态类存放下面两个方法 2、生成验证码主要是通过创建一个随机生成数跟字母 3、给验证码添加一些干扰,也就是清晰度的问题,看起来迷糊迷糊就行 4、调用静态类的方法生成图片返回给视图img 一、控制器生成方法 1、创建静态类ValidCodeUtils 2、生成随机数 Static 静态的方法

我要记录学习博客 1978

[C#] 生成验证码

C#实现生成验证码功能,具体步骤如下:第一步,创建一个新的页面Code.aspx,在.cs文件中添加下面的代码:using System.IO;using System.Drawing.Imaging;using System.Drawing.Drawing2D;using System.Drawing;public partial class Code : System.Web.U

有了大树的幸福树懒~~~的专栏 1578

C#使用GDI+实现生成验证码

这个程序的主要功能是生成一个包含随机字符和干扰线的验证码图像,并允许用户输入验证码进行验证。它使用了Graphics类来绘制图像,并在用户操作(如点击图片或验证按钮)时响应事件。

2301_79849777的博客 1801

c# 生成中文验证码

 用C#生成中文汉字验证码的基本原理      在做一个网站登陆时,因客户的要求,需要生成中文汉字验证码,于是便在网上找现成的程序,果然找到了,可是对里面很多东西和原理不熟悉,于是查找资料和动手实践,终于弄明白C#生成中文汉字验证码是怎么回事了,本菜鸟就将学习过程和心得写在这里供有同样需求的同行学习。  下面就来介绍一下使用C#生成随机的中文汉字的原理

serbet的专栏 535

C# 生成验证码图片

System;using System.Collections.Generic;using System.Text;using System.Threading;using System.Drawing;using System.Drawing.Drawing2D; namespace FreeOH.Validate{    ///     /// 本类用于产生随机的验证码    ///   

fisea的专栏 608

C# 生成验证码

验证码的作用嘛,基本就是用来防暴力破解,防恶意攻击、注册; 即便现在人工智能的高潮兴起,OCR的广泛应用;许多的验证码已经形同虚设,但是做一个项目,该有的基础验证我还是得有;你破你的,我防我的,大家开心就行。 一个简单的验证码生成方法; 实现功能: 随机生成验证码显示到pictureBox 开发环境: 开发工具: Visual Studio 2013 .NET Framework版本:4.5 实现代码: private void GenerateCode(int code

qq_27410185的博客 3287

c# 生成 验证码

/// <summary> /// 生成验证码图片 /// </summary> public void CreateCheckCodeImage() { string checkCode = VerficationCodeSrc(5); if (checkCode == null || checkCode.Trim() == String...

weixin_30295091的博客 69

C#生成验证码图片

using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System...

u011511086的专栏 380
上一篇: ip查询
下一篇: ASP.NET C# 发送邮件
woaiprogram
博客等级 码龄20年 17粉丝 3原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值