zoukankan      html  css  js  c++  java
  • c#_生成图片式验证码

    废话不多说直接上代码。

     1     class Check_Code
     2     {
     3         /// <summary>
     4         /// 生成随机验证码数字+字母
     5         /// </summary>
     6         /// <param name="codelen">验证码长度</param>
     7         /// <returns>返回验证码</returns>
     8         public static string MakeCode(int codelen)
     9         {
    10             if (codelen < 1)
    11             {
    12                 return string.Empty;
    13             }
    14             int number;
    15             StringBuilder strCheckCode = new StringBuilder();
    16             Random random = new Random();
    17             for (int index = 0; index < codelen; index++)
    18             {
    19                 number = random.Next();
    20                 if (number % 2 == 0)
    21                 {
    22                     strCheckCode.Append((char)('0' + (char)(number % 10)));//生成随机数字
    23                 }
    24                 else
    25                 {
    26                     strCheckCode.Append((char)('A' + (char)(number % 26)));//生成随机字母
    27                 }
    28             }
    29             return strCheckCode.ToString();
    30         }
    31         public static MemoryStream CheckCodeImage(string CheckCode)
    32         {
    33             if (string.IsNullOrEmpty(CheckCode))
    34             {
    35                 return null;
    36             }
    37             Bitmap image = new Bitmap((int)Math.Ceiling((CheckCode.Length * 12.5)), 22);
    38             Graphics graphic = Graphics.FromImage(image);//创建一个验证码图片
    39             try
    40             {
    41                 Random random = new Random();
    42                 graphic.Clear(Color.White);
    43                 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    44                 for (int index = 0; index < 25; index++)
    45                 {
    46                     x1 = random.Next(image.Width);
    47                     x2 = random.Next(image.Width);
    48                     y1 = random.Next(image.Height);
    49                     y2 = random.Next(image.Height);
    50                     graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
    51                 }
    52                 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));//Font设置字体,字号,字形
    53                 //设置图形渐变色的起始颜色与终止颜色,渐变角度
    54                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
    55                 graphic.DrawString(CheckCode, font, brush, 2, 2);
    56                 int X = 0; int Y = 0;
    57                 //绘制图片的前景噪点
    58                 for (int i = 0; i < 100; i++)
    59                 {
    60                     X = random.Next(image.Width);
    61                     Y = random.Next(image.Height);
    62                     image.SetPixel(X, Y, Color.FromArgb(random.Next()));
    63                 }
    64                 //画图片的边框线
    65                 graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
    66                 //将图片保存为stream流返回
    67                 MemoryStream ms = new MemoryStream();
    68                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    69                 return ms;
    70             }
    71             finally
    72             {
    73                 graphic.Dispose();
    74                 image.Dispose();
    75             }
    76         }
    77     }

     生成验证码图片,需要先生成验证码,再将验证码处理为图片。

    转载引用黎木博客-https://www.cnblogs.com/running-mydream/p/4071528.html

  • 相关阅读:
    像素图生成向量图的算法
    黑白图着色(转换成彩色图片)的算法
    JavaScript的DOM编程--12--innerHTML属性
    JavaScript的DOM编程--11--插入节点
    JavaScript的DOM编程--10--删除节点
    JavaScript的DOM编程--09--节点的替换
    js中return、return true、return false的区别
    JavaScript的DOM编程--08--复习
    JavaScript的DOM编程--07--节点的属性
    JavaScript的DOM编程--06--两个实验
  • 原文地址:https://www.cnblogs.com/dobiprogrammer/p/9722412.html
Copyright © 2011-2022 走看看