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 )
        
{                
            
forint i = 0 ; i < 8 ; i++ )
            
{
                
forint 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
    }

}




 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值