我写了一个验证码生成程序,现公开算法,哪位能做识别
代码是可以直接编译的,新建一个WinForm程序,把Program.cs文件的内容替换为这里的代码即可
应该比没公开算法的简单些,要求就是实现BytesToString函数using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
static class Program
{
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
        Button btn = new Button();
        btn.Text = "刷新";
        btn.Click += btn_Click;
        Form frm = new Form();
        FlowLayoutPanel panel = new FlowLayoutPanel();
        panel.Controls.Add(pic);
        panel.Controls.Add(btn);
        Button btnCode = new Button();
        btnCode.Text = "识别";
        btnCode.Click += btnCode_Click;
        panel.Controls.Add(btnCode);
        frm.Controls.Add(panel);
        Application.EnableVisualStyles();
        Application.Run(frm);
    }    static void btnCode_Click(object sender, EventArgs e)
    {
        MemoryStream ms = new MemoryStream();
        pic.Image.Save(ms, ImageFormat.Gif);
        byte[] bs = ms.GetBuffer();
        string code = BytesToString(bs);
        MessageBox.Show(code);
    }    static PictureBox pic = new PictureBox();    public static string BytesToString(byte[] bs)
    {
        throw new NotImplementedException("尚未识别,占个位");//实现该函数
    }
    public static byte[] StringToBytes(string strRandom)
    {
        Color[] colorArray = new Color[] { Color.DarkViolet, Color.Red, Color.Purple, Color.Maroon, Color.Salmon, Color.OrangeRed, Color.Orange, Color.Olive, Color.OliveDrab, Color.GreenYellow, Color.HotPink, Color.LawnGreen, Color.Lime, Color.LightSeaGreen, Color.Blue, Color.Brown };
        Color[] colorArray2 = new Color[] { Color.Black, Color.Azure, Color.White, Color.DimGray };
        Random random = new Random();
        string familyName = "Arial";
        int num = 12;
        int width = 90;
        int height = 30;
        Color color = colorArray[random.Next(0, 15)];
        Color color2 = colorArray2[random.Next(0, 3)];
        FontStyle[] styleArray2 = new FontStyle[4];
        styleArray2[0] = FontStyle.Italic;
        styleArray2[2] = FontStyle.Strikeout;
        styleArray2[3] = FontStyle.Underline;
        FontStyle[] styleArray = styleArray2;
        Font font = new Font(familyName, (float)num, FontStyle.Bold | styleArray[random.Next(0, 3)]);
        Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        Graphics graphics = Graphics.FromImage(image);
        Rectangle rect = new Rectangle(0, 0, width, height);
        graphics.FillRectangle(new SolidBrush(color), rect);
        Pen pen = new Pen(colorArray2[random.Next(0, 3)], 1f);
        Point point = new Point(random.Next(0, 13), random.Next(-2, 0x11));
        Point point2 = new Point(random.Next(0x48, 0x4e), random.Next(-2, 0x11));
        graphics.DrawLine(pen, point, point2);
        graphics.DrawString(strRandom.Substring(0, 1), font, new SolidBrush(color2), (float)random.Next(0, 13), (float)random.Next(-2, 0x11));
        color2 = colorArray2[random.Next(0, 3)];
        graphics.DrawString(strRandom.Substring(1, 1), font, new SolidBrush(color2), (float)random.Next(0x15, 30), (float)random.Next(-2, 0x11));
        color2 = colorArray2[random.Next(0, 3)];
        graphics.DrawString(strRandom.Substring(2, 1), font, new SolidBrush(color2), (float)random.Next(0x26, 0x2f), (float)random.Next(-2, 0x11));
        color2 = colorArray2[random.Next(0, 3)];
        graphics.DrawString(strRandom.Substring(3, 1), font, new SolidBrush(color2), (float)random.Next(0x37, 0x40), (float)random.Next(-2, 0x11));
        color2 = colorArray2[random.Next(0, 3)];
        graphics.DrawString(strRandom.Substring(4, 1), font, new SolidBrush(color2), (float)random.Next(0x48, 0x4e), (float)random.Next(-2, 0x11));
        MemoryStream stream = new MemoryStream();
        image.Save(stream, ImageFormat.Gif);
        graphics.Dispose();
        image.Dispose();
        byte[] bs = stream.GetBuffer();
        return bs;
    }
    static void btn_Click(object sender, EventArgs e)
    {
        //随机函数取下界而不取上界,取值范围10000--99999
        pic.Image = Image.FromStream(new MemoryStream(StringToBytes(new Random().Next(10000, 100000).ToString())));
    }
}