C#取验证码

发布时间:2016-12-6 22:22:02 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"C#取验证码",主要涉及到C#取验证码方面的内容,对于C#取验证码感兴趣的同学可以参考一下。

using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Net;using System.Net.Sockets;namespace LeadBBSEnder{    /// <summary>    /// ValidateCode 的摘要说明。    /// </summary>    public sealed class RunningRes    {        private static TcpClienter _tc;        public static TcpClienter Tcp        {            get            {                if( _tc == null )                    _tc = new TcpClienter( string.Empty , new IPEndPoint(IPAddress.Any , 1710) , new IPEndPoint(IPAddress.Parse( "221.12.79.162" ) , 80) );                return _tc;            }        }                public static string GetValidateCode( Bitmap bm )        {            short s1 = Paste( bm , Position.Char1 );            short s2 = Paste( bm , Position.Char2 );            short s3 = Paste( bm , Position.Char3 );            short s4 = Paste( bm , Position.Char4 );            return string.Format("{0}{1}{2}{3}" , s1 , s2 , s3 , s4 );        }        private static Point GetOffsetPoint( Position p )        {            switch( p )            {                case Position.Char1:                    return new Point( 2 , 1 );                case Position.Char2:                    return new Point( 9 , 1 );                case Position.Char3:                    return new Point( 16 , 1 );                default:                    return new Point( 23 , 1 );            }        }        private static short Paste( Bitmap bm , Position pc )        {            short[,] Char0 = {                    { 0,1,1,1,1,0 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,1,1,1,1,0 }                             };            short[,] Char1 = {                    { 0,0,1,1,0,0 },                    { 0,1,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,1,1,1,1,0 }                             };            short[,] Char2 = {                    { 0,1,1,1,1,0 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,0,0,1,1,0 },                    { 0,0,1,1,0,0 },                    { 0,1,1,0,0,0 },                    { 1,1,0,0,0,0 },                    { 1,1,1,1,1,1 }                             };            short[,] Char3 = {                    { 0,1,1,1,1,0 },                    { 1,1,0,0,1,1 },                    { 0,0,0,0,1,1 },                    { 0,0,1,1,1,0 },                    { 0,0,0,0,1,1 },                    { 0,0,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,1,1,1,1,0 }                             };            short[,] Char4 = {                    { 0,0,0,1,1,0 },                    { 0,0,1,1,1,0 },                    { 0,1,1,1,1,0 },                    { 0,1,1,1,1,0 },                    { 1,1,0,1,1,0 },                    { 0,1,1,1,1,1 },                    { 0,0,0,1,1,0 },                    { 0,0,0,1,1,1 }                             };            short[,] Char5 = {                    { 1,1,1,1,1,1 },                    { 1,1,0,0,0,0 },                    { 1,1,0,0,0,0 },                    { 1,1,1,1,1,0 },                    { 0,0,0,0,1,1 },                    { 0,0,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,1,1,1,1,0 }                             };            short[,] Char6 = {                    { 0,1,1,1,1,0 },                    { 1,1,0,1,1,0 },                    { 1,1,0,0,0,0 },                    { 1,1,1,1,1,0 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,1,1,1,1,0 }                             };            short[,] Char7 = {                    { 1,1,1,1,1,1 },                    { 1,1,0,1,1,0 },                    { 0,0,0,1,1,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 },                    { 0,0,1,1,0,0 }                             };            short[,] Char8 = {                    { 0,1,1,1,1,0 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,1,1,1,1,0 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,1,1,1,1,0 }                             };            short[,] Char9 = {                    { 0,1,1,1,1,0 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 1,1,0,0,1,1 },                    { 0,1,1,1,1,1 },                    { 0,0,0,0,1,1 },                    { 0,1,1,0,1,1 },                    { 0,1,1,1,1,0 }                             };            Point Sp = GetOffsetPoint( pc );            if( Paste(bm,Sp,Char0) )                return 0;            if( Paste(bm,Sp,Char1) )                return 1;            if( Paste(bm,Sp,Char2) )                return 2;            if( Paste(bm,Sp,Char3) )                return 3;            if( Paste(bm,Sp,Char4) )                return 4;            if( Paste(bm,Sp,Char5) )                return 5;            if( Paste(bm,Sp,Char6) )                return 6;            if( Paste(bm,Sp,Char7) )                return 7;            if( Paste(bm,Sp,Char8) )                return 8;            if( Paste(bm,Sp,Char9) )                return 9;            throw new Exception("解释不到!");        }        private static bool Paste( Bitmap bm , Point offset, short[,] Char )        {                            for( int i = 0 ; i < 8 ; i++ )            {                for( int j = 0 ; j < 6 ; j++ )                {                    Color Pc = bm.GetPixel( offset.X + j , offset.Y + i );                    if( Pc.A == 255 && Char[i,j] == 1 || Pc.A == 0 && Char[i,j] == 0 )                        continue;                    else                        return false;                }            }            return true;        }    }    internal enum Position    {        Char1,Char2,Char3,Char4    }} 

上一篇:自己收藏的SQL语句
下一篇:HANDLE,HMODULE,HINSTANCE,HWND概念

相关文章

关键词: C#取验证码

相关评论