zoukankan      html  css  js  c++  java
  • 图形验证码

    新建一个.cs文件用来写生成图形验证码的方法,返回一个MemoryStream的参数。在Default.aspx中接收输出就可以了。

    1.下面是.cs的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    
    /// <summary>
    ///BitmapImage 的摘要说明
    /// </summary>
    public class BitmapImage
    {
    
        public static MemoryStream ImageCheck()
        {
            Random random = new Random();
            //随机产生4位随机数
            string checkCode = random.Next(1000, 9999).ToString();
            //Bitmap是用来指定初始化时文本的大小
            Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 20.5)), 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
    
                g.Clear(Color.White);//清空图片背景色
                //画图片的背景噪音线这里是4条线
                for (int i = 0; i < 4; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Black), x1, x2, y1, y2);
                }
                Font font = new Font("Arial", 12, FontStyle.Bold);
                //设置颜色,起始位置等参数
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);
                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片噪音线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                MemoryStream ms = new MemoryStream();
                image.Save(ms, ImageFormat.Gif);
                return ms;
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
    
        }
    }
    

    2.以下是Default.aspx.cs中的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MemoryStream ms= BitmapImage.ImageCheck();
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
    }
    

     3.截图

     

  • 相关阅读:
    【重点推荐】五美凡生论
    语言哲学宣言2018
    四要同环图
    知识分子必须毫不留情反对一切“教养阶层”
    世界上任何一件事的五个模块
    Web 在线制表工具稳定吗?和桌面报表工具对比哪个好用?
    Web 在线制表工具稳定吗?和桌面报表工具对比哪个好用?
    有没有简单易用的数据挖掘工具?
    BI、OLAP、多维分析、CUBE 这几个词是什么关系?
    传说中的中国复杂报表都长什么样?有什么特点?
  • 原文地址:https://www.cnblogs.com/hp-discuz/p/5141588.html
Copyright © 2011-2022 走看看