• C# 验证码


    作者:陈太汉

    C# 验证码

    多功能注册码,注册码可以完全自定义,全部都是动态的,包括字体的颜色,大小,样式,还有内容

    using System;
    using System.Drawing;

    namespace SecurityCode
    {
    public class DrawMethod
    {

    /// <summary>
    /// 画图
    /// </summary>
    /// <param name="content"></param>
    /// <param name="size"></param>
    /// <param name="fileName"></param>
    public void Draw(string content,Size size,string fileName)
    {
    Image image
    = new Bitmap(size.Width,size.Height);
    Graphics g
    = Graphics.FromImage(image);
    DrawBorder(g,image.Size);
    DrawContent(g,content,image.Size,(
    int)FontSet.Font.Size);
    DrawRandom(g, image.Size);
    image.Save(
    @"D:\\" + fileName);
    }

    /// <summary>
    /// 画边框
    /// </summary>
    /// <param name="g"></param>
    /// <param name="size"></param>
    private void DrawBorder(Graphics g, Size size)
    {
    Pen pen
    = new Pen(SolidBrushSet.SolidBrush);
    Rectangle rect
    =new Rectangle(1,1,size.Width-4,size.Height-4);
    g.DrawRectangle(pen,rect);
    }

    //画字符串
    private void DrawContent(Graphics g, string content,Size size,int fontHeight)
    {
    Point point
    = new Point();
    int i = 0;
    point.Y
    = (size.Height - fontHeight) / 2;
    int distance = size.Width / (content.Length+1);
    foreach (char c in content)
    {
    point.X
    = i * distance + distance/2;
    g.DrawString(c.ToString(), FontSet.Font, SolidBrushSet.SolidBrush, point);
    i
    ++;
    }
    }

    /// <summary>
    /// 画干扰
    /// </summary>
    /// <param name="g"></param>
    /// <param name="size"></param>
    private void DrawRandom(Graphics g, Size size)
    {
    Pen pen
    = new Pen(SolidBrushSet.SolidBrush);
    Random rand
    = new Random();
    for (int i = 0; i < 3; i++)
    {
    g.DrawLine(pen, rand.Next(size.Width), rand.Next(size.Width), rand.Next(size.Height), rand.Next(size.Height));
    }

    for (int i = 0; i < 10; i++)
    {
    Rectangle rect
    = new Rectangle(rand.Next(2, size.Width - 2), rand.Next(2, size.Height - 2), 1, 1);
    g.DrawRectangle(pen, rect);
    }
    }

    }
    }

    using System;

    namespace SecurityCode
    {
    /// <summary>
    /// 自动生成字符串
    /// </summary>
    public class CreateContent
    {
    private string so = "1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM";

    /// <summary>
    /// 生成内容
    /// </summary>
    /// <returns></returns>
    public string GetContent()
    {
    Random rand
    = new Random();
    string str = null;
    for (int i = 0; i < 6; i++)
    {
    str
    += so.Substring(rand.Next(62), 1);
    }
    return str;
    }
    }
    }

    using System;
    using System.Drawing;

    namespace SecurityCode
    {
    /// <summary>
    /// 字体设置
    /// </summary>
    public class FontSet
    {
    public static Font Font
    {
    get
    {
    FontFamily fFamily
    = GetFontFamily();
    FontStyle fStyle
    = GetFontStyle();
    int emSize = GetFontSize();
    return new Font(fFamily, emSize, fStyle);
    }
    }

    private FontSet() { }


    /// <summary>
    /// 设置字体
    /// </summary>
    private static FontFamily GetFontFamily()
    {
    Random rand
    = new Random();
    return FontFamily.Families[rand.Next(FontFamily.Families.Length)];
    }

    /// <summary>
    /// 设置字体样式
    /// </summary>
    private static FontStyle GetFontStyle()
    {
    Random rand
    = new Random();
    int index = rand.Next(1, 4);
    index
    = 1 << index;
    return (FontStyle)index;
    }

    /// <summary>
    /// 设置字体大小
    /// </summary>
    public static int GetFontSize()
    {
    Random rand
    = new Random();
    return rand.Next(12, 14);
    }
    }
    }

    using System;
    using System.Drawing;

    namespace SecurityCode
    {
    /// <summary>
    /// 画笔设置
    /// </summary>
    public class SolidBrushSet
    {
    /// <summary>
    /// 画笔
    /// </summary>
    public static SolidBrush SolidBrush
    {
    get {
    Color color
    = GetColor();
    return new SolidBrush(color);
    }
    }

    private SolidBrushSet(){}

    /// <summary>
    /// 随机生成画笔颜色
    /// </summary>
    /// <returns></returns>
    public static Color GetColor()
    {
    Random rand
    = new Random();
    int r = rand.Next(0,255);
    int g = rand.Next(0, 255);
    int b = rand.Next(0, 255);
    return Color.FromArgb(r,g,b);
    }
    }
    }

    private void Test()
    {
    DrawMethod drawMethod
    = new DrawMethod();
    CreateContent createCont
    = new CreateContent();
    for (int i = 0; i < 10; i++)
    {
    string content = createCont.GetContent();
    Size size
    = new Size(100, 40);
    drawMethod.Draw(content, size, i.ToString()
    + ".jpg");
    }
    }
  • 相关阅读:
    【剑指Offer】面试题14- I. 剪绳子
    【剑指Offer】面试题56
    【LeetCode】202. 快乐数
    【LeetCode】1095. 山脉数组中查找目标值
    【LeetCode】260. 只出现一次的数字 III
    【剑指Offer】面试题56
    【LeetCode】33. 搜索旋转排序数组
    【LeetCode】23. 合并K个排序链表
    【LeetCode】46. 全排列
    mysql可视化工具下载地址2017.6.27
  • 原文地址:https://www.cnblogs.com/hlxs/p/2087977.html
Copyright © 2020-2023  润新知