很久以前开心网很火时候的群发代码

很久以前开心网很火时候的群发代码
为了清理硬盘 哈哈  代码要干掉了  传网络上共享下。。。哈哈

包含群发和验证码识别  自己看吧  乱的  不是好东西  呵呵  有问题就 企Q鹅 362.505.707

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text.RegularExpressions;
public class KX
{

    public static void Show(string str)
    {
        Console.WriteLine(string.Format("{0} {1}", DateTime.Now.ToString("HH:mm:ss fff"), str));//
       // f.ShowLog(str);//
    }
    static  CodeDiscriminator cd=null;
    static string Data = "attachment_random=&attachment_cancel=&attachment_forwarding=&forward_thread=0" +
        "&uids={0}&rcode={1}&service=0&content={2}&texttype=html&code={3}";
    static string CODE_URL = "http://www.kaixin001.com/interface/regcreatepng.php?randnum=";
    public string RCode = "0.40902566815883223_1251100656109";
    public string Cookies = "_uid=2x; _email=zfrong2000; _kx=xxx _user=xxx; presence=ZHlPQ-kHBZw9-W4JajqxNyn5em.MjQ5NzE3MQ; _ref=.B346; _invisible=0";
    public string UID;
    public string Content;
    public string Code;
    public string Area="";
    static string Send_URL = "http://www.kaixin001.com/msg/post.php";
    public void Init()
    {
        Show("开始->初始化中...稍等");//
        if (cd == null)
        {
            cd = new CodeDiscriminator();
            cd.Init();//
        }
    }
    public void Post()
    {
        string data=string.Format(Data,UID,RCode,Content,Code);
        string rtn = Zfr.Common.Helper.PostForm(Send_URL, data, false, Cookies, Encoding.GetEncoding("utf-8"), Send_URL, null);
        Show(rtn);
    }
    public void Login(string user, string pass)
    {
        user = user.Replace("@","%40");
        string url = "http://www.kaixin001.com/login/login.php";
        string data = string.Format("email={0}&password={1}&remember=1&invisible_mode=0&rcode=&rpkey=&diarykey=&code=&url=/home/", user, pass);
        string cookies = "SERVERID=_srv100-81_;_ref=.;";
        Zfr.Common.Helper.PostForm(url, data, false, cookies, Encoding.GetEncoding("utf-8"), "http://www.kaixin001.com/login/", null);
        Show("Cookies"+Zfr.Common.Helper.Cookies);
        this.Cookies = Zfr.Common.Helper.Cookies;
    }
    public void AddMyFF()
    {
        AddMyFF(0);
    }
    public void AddMyFF(int startIndex)
    {
        string url = "http://www.kaixin001.com/friend/index.php?&viewtype=&start=";
        int start = startIndex-startIndex%40;
        bool b = true;
        string rtn;
        int index = 0; MatchCollection mColl;
        do
        {
            Show("Start->MyF:" + start);//
            Show("开始->Get");
            rtn = Zfr.Common.Helper.GetPage(url + start, Cookies, Encoding, url);
            Show("返回->Get");
            mColl = Regex.Matches(rtn, @"uid=(\d+?)"" title=""(.*?)""", regOpt);
            foreach (Match m in mColl)
            {
                index++; if (startIndex >= index) continue;
                Show("Start->MyF:------------------------------------" + index + "-------------------------------------------------");//
                AddMyFF(m.Groups[1].Value.Trim(), m.Groups[2].Value.Trim());
            }
            start += 40; b = (mColl != null && mColl.Count != 0);
            rtn = null; mColl = null;
        } while (b);
        url = null;
        Show("END->MyF");//
    }
     public void AddMyFF(string fid)
    {
          AddMyFF(fid,"");
     }
     void AddMyFF(string fid,string fName)
    {
        string url =string.Format("http://www.kaixin001.com/friend/?uid={0}&viewtype=&start=",fid);
        int start = 0;
        bool b = true;
        string rtn,rtn2;
        int count = 0; MatchCollection mColl; string mm = ""; Match m1,m2;
        do
        {
            Show("Start->MyFF:------------------------------------" + start+"-------------------------------------------------------");//
            Show("开始->Get");
            rtn = Zfr.Common.Helper.GetPage(url + start, Cookies, Encoding, url);
            Show("返回->Get");
            mColl = Regex.Matches(rtn,@"<div class=""fl_area"">(.*?)<div class=""c""></div>",regOpt);// @"uid=(\d+?)""  title=""(.*?)""", regOpt);
            
            foreach (Match m in mColl)
            {
                rtn2 = m.Groups[1].Value.Trim();
                
                m2 = Regex.Match(rtn2,@"uid=(\d+?)""  title=""(.*?)""", regOpt);
                if (m2.Success)
                {
                    if (Area != null && Area.Length != 0)
                    {
                        m1 = Regex.Match(rtn2, @"<p>(.*?)</p>", regOpt);
                        if (m1.Success)
                        {
                            mm = m1.Groups[1].Value.Replace("\t", "").Replace("\r\n", "");
                            if (mm.IndexOf("" + Area) == -1)
                            {
                                Show("ADD->FID:" + m2.Groups[1].Value.Trim() + " " + mm + " 地区不符合!");
                                continue;
                            }
                        }
                    }
                    mm = m2.Groups[1].Value.Trim() + "','0','1','0'";
                    if (rtn.IndexOf(mm) != -1)
                    {
                        AddFriend(m2.Groups[1].Value.Trim(), fName + "->" + m2.Groups[2].Value.Trim());
                    }
                    else
                    {
                        Show("ADD->FID:" + m2.Groups[1].Value.Trim() + " " + fName + "->" + m2.Groups[2].Value.Trim() + " 已是你好友!");
                    }
                }
            }

            start += 20;
            if (start ==20)
            {
               m2=Regex.Match(rtn, @"她共有(\d+?)位好友", regOpt);
               if (m2.Success)
               {  
                   int.TryParse(m2.Groups[1].Value,out count);
                   Show("ADD->FID:" + m2.Groups[1].Value.Trim() + " 共有好友" +count);
               }
            }
            b = (start < count) && (mColl != null) && (mColl.Count != 0);
            rtn = ""; rtn2 = ""; mm = null; m1 = null; m2 = null;
        } while (b);
    }
//    public void AddFF()
//    {
//        string url="http://www.kaixin001.com/friend/search.php?stype=1&start=";
//        int start = 0;
//        bool b = true;
//        string rtn;
//        do
//        {
//            Show("开始->Start:" + start);//
//            rtn = Zfr.Common.Helper.GetPage(url+start,Cookies, Encoding,url);
//            MatchCollection mColl = Regex.Matches(rtn, @"uid=(\d+?)"" title=""(.*?)""", regOpt);
//            foreach (Match m in mColl)
//            {
//                AddFriend(m.Groups[1].Value.Trim(), m.Groups[2].Value.Trim());
//            }
//            start += 20; b = (mColl != null && mColl.Count != 0);
//        } while (b);
//    }
    static Encoding Encoding = Encoding.UTF8;
    static string FData="from=&touid={0}&content={1}&rcode={2}&code={3}&usercode=&email=&bidirection=";
    private static RegexOptions regOpt = RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Singleline;//表达式操作
     public void AddFriend(string uId)
    {
       AddFriend( uId,"");
     }
      void AddFriend(string uId,string uName)
    {
        if (Hashtable.ContainsKey(uId))
        {
            Show("ADD->FID:" + uId + " " + uName+" 发送过请求啦!");
            return;
        }
        else
            Hashtable.Add(uId, null);
        string url = "http://www.kaixin001.com/friend/addverify.php";
        string data; string rtn; Match m;
        abc:
        Show("ADD->FID:" + uId + " " + uName);
        data = string.Format(FData, uId, Content, RCode, GetCodeP());
        Show("开始->POST");
        rtn = Zfr.Common.Helper.PostForm(url, data, false, Cookies, Encoding, url, null);
        Show("返回->POST");
        m = Regex.Match(rtn, @"var type = ""(\d+?)""", regOpt);
        if (m.Success)
            Show("返回->成功码:" + m.Groups[1].Value);//
        if (m.Groups[1].Value == "3")
            goto abc;

        if (m.Groups[1].Value == "5")
            Show("返回->你的好友数已超过1000,不再可以加别人为好友!");
        else if (m.Groups[1].Value == "8")
             Show("返回->它的好友已达上限,不能再加新的好友!");
        else
          System.Threading.Thread.Sleep(100);//

        data = null; rtn = null; m = null; url = null;
    }
    static System.Collections.Hashtable Hashtable = new System.Collections.Hashtable();
    public string GetCodeP()
    {
        string c="";
        while(c.Length!=4)
        {
            c = GetCode();
        }
        return c;
    }
    public string  GetCode()
    {
        string c = "";
        Show("获取->验证码");
        Bitmap img= Zfr.Common.Helper.GetImage(CODE_URL+RCode,Cookies,CODE_URL);
        Show("返回->验证码");
        if (img != null)
        {
            cd.Bitmap = img;
            //cd.Bitmap.Save("C:\\"+DateTime.Now.ToString()+".gif");
            Show("开始->识别");
            cd.Analyse();
            c = cd.AnalysedCodesString;
            Show("识别->验证码:" + c);
        }
        return c;
    }

    public static void AddGroup(string cookies,string name,int startIndex)
    {
        string url = "http://www.kaixin001.com/group/search.php?start={0}&name=" + System.Web.HttpUtility.UrlEncode(name);
        int start = startIndex-startIndex%40;
        bool b = true;
        string rtn;
        int index = 0; MatchCollection mColl;
        do
        {
            Show("Start->Search Group:" + start);//
            Show("开始->Get");
            rtn = Zfr.Common.Helper.GetPage(string.Format(url, start), cookies, Encoding, string.Format(url, start));
            Show("返回->Get");
            mColl = Regex.Matches(rtn, @"joingroup\((\d+?)\)", regOpt);
            foreach (Match m in mColl)
            {
                index++; if (startIndex >= index) continue;
                b=AddGroup(cookies, m.Groups[1].Value.Trim());
            }
            start += 10; b =b& (mColl != null && mColl.Count != 0);
            rtn = null; mColl = null;
        } while (b);
        url = null;
        Show("END->Search Group");//
    }
    private static bool AddGroup(string cookies, string groupIdStr)
    {
        int groupId = 0;
        int.TryParse(groupIdStr, out groupId);
       return AddGroup(cookies, groupId);
    }
    private static bool AddGroup(string cookies, int groupId)
    {

        string url = "http://www.kaixin001.com/group/join.php?jgid=";
        string data = "apply=1&jgid={0}&content=加我啊老大!!!";
        string rtn = "";
            Show("Start->Add Group:" + groupId);//
            Show("开始->Post");
            rtn = Zfr.Common.Helper.PostForm(url + groupId,string.Format(data,groupId),false, cookies, Encoding.UTF8, url,null);
            Show("返回->Post");
            Match m = Regex.Match(rtn, @"var v_type = ""(\d+?)""", regOpt);
            bool b = true;
         if (m.Success)
         {
             Show("返回->成功码:" + m.Groups[1].Value);//
             if (m.Groups[1].Value == "1")
                 Show("返回->您的申请已经提交,请耐心等待管理员审批。");
             else  if (m.Groups[1].Value == "2")
                 Show("返回->您已经成功的加入了群");
             else if (m.Groups[1].Value == "3")
                 Show("返回->您已经是群成员");
             else if (m.Groups[1].Value == "4")
                 Show("返回->该群人数已达到上限,无法再加入新成员");
             else if (m.Groups[1].Value == "5")
             { Show("返回->你今天加入的群数过多(每日最多加入20个群)!"); b = false; }
             else if (m.Groups[1].Value == "6")
             { Show("返回->你加入的群数已达到上限(1024个),不能再加入群了!"); b = false; }
         }
         rtn = null; data = null; url = null;
         return b;
    }
    public static void AgreeFriends(string cookies)
    {
        AgreeFriends(cookies, 0);
    }
    public static void AgreeFriends(string cookies, int startIndex)
    {
        string url = "http://www.kaixin001.com/msg/sys.php?type=1&start=",
            url2 = "http://www.kaixin001.com/friend/editfriend.php";
        string rtn="";
        string v1, v2;
        string data = "fuid={0}&byname=&memo=&groups=&start=0&sysmsgtype=1&from=sysmsg&smid={1}";
        bool b = true;
        do
        {
            Show("开始->Get");
            rtn = Zfr.Common.Helper.GetPage(url+startIndex, cookies, Encoding.UTF8, url);
            Show("返回->Get");
            MatchCollection mColl = Regex.Matches(rtn, @"agreefriend\((\d+?), (\d+?)\)", regOpt);
            foreach (Match m in mColl)
            {
                if (m.Success)
                {
                    v1 = m.Groups[1].Value;
                    v2 = m.Groups[2].Value;
                    //url2 = "http://www.kaixin001.com/friend/editfriend_dialog.php?smid={0}&fuid={1}";
                    //rtn = Zfr.Common.Helper.GetPage(string.Format(url2,v2,v1), cookies, Encoding.UTF8, url);
                    //fuid=59x&byname=&memo=&groups=&allgroups=%E7%8E%B0%E5%9C%A8%E5%90%8C%E4%BA%8B%2C%E4%BB%A5%E5%89%8D%E5%90%8C%E4%BA%8B%2C%E5%A4%A7%E5%AD%A6%E5%90%8C%E5%AD%A6%2C%E9%AB%98%E4%B8%AD%E5%90%8C%E5%AD%A6%2C%E5%AE%B6%E4%BA%BA%E4%BA%B2%E6%88%9A%2C%E6%8C%9A%E4%BA%A4%E5%A5%BD%E5%8F%8B%2C%E6%99%AE%E9%80%9A%E6%9C%8B%E5%8F%8B%2C%E6%9C%8B%E5%8F%8B%E7%9A%84%E6%9C%8B%E5%8F%8B%2C%E5%85%B6%E4%BB%96&start=0&sysmsgtype=1&from=sysmsg&smid=10447034181
                    Zfr.Common.Helper.PostForm(url2, string.Format(data, v1, v2), false, cookies, Encoding.UTF8, url, null);
                }
            }
            startIndex += 10; System.Threading.Thread.Sleep(1000);
            if (rtn.IndexOf("下一页") == -1)
                b = false;
        } while (b&startIndex < 3000);
    }
    public static void PostMSG(string cookies, string content)
    {
        string ids="";
        int count=Friends.Count;
        Show("开始->消息群发");
        for(int i=0;i<count;i++)
        {
            ids +=","+Friends[i];
            if (i % 20 == 19||i+1==count)
            {
                ids = ids.Substring(1);
                PostMSG(cookies, ids, content);
                ids = "";
            }
        }
        Show("结束->消息群发");
    }
    public static void InitFrends(string cookies)
    {
        string url = "http://www.kaixin001.com/friend/?viewtype=&start=";
        int start = 0;
        bool b = true;
        string rtn;
        MatchCollection mColl; int add = 0;
        Show("开始->压入好友");//
        do
        {
            Show("Start->MyF:" + start);//
            Show("开始->Get");
            rtn = Zfr.Common.Helper.GetPage(url + start, cookies, Encoding, url);
            Show("返回->Get");
            mColl = Regex.Matches(rtn, @"uid=(\d+?)""",regOpt);//
            foreach (Match m in mColl)
            {
                if (m.Success)
                {
                    rtn=m.Groups[1].Value;
                    if (!Friends.Contains(rtn))
                    {
                        Show("压入->Friend:"+rtn);
                        Friends.Add(rtn); add++;
                    }
                }
            }
            start += 40; b = (mColl != null && mColl.Count != 0); b = b & (add != 0);
            rtn = null; mColl = null; add = 0;
        }while(b);
        Show("结束->压入好友");//
    }
    static System.Collections.Generic.IList<string> Friends = new System.Collections.Generic.List<string>();
   static void PostMSG(string cookies,string ids, string content)
    {
        string url = "http://www.kaixin001.com/msg/post.php";
        string data="attachment_random=&attachment_cancel=&attachment_forwarding=&forward_thread=&uids={0}&rcode=&service=0&group=&content={1}&texttype=html&code=";
        Zfr.Common.Helper.PostForm(url, string.Format(data, ids, content), false, cookies, Encoding, url, null);
        data = null; ids = null;
   }
}

public class GanJi
{
    static  CodeDiscriminator cd = new CodeDiscriminator();
    static GanJi() { cd.Init(); }
    public static string GetCode(string cookies )
    {
        string c = "";
        TimeSpan ts=(TimeSpan)(DateTime.Now- new DateTime(1970,01,01));
        string s = ts.TotalSeconds.ToString();
        if (s.IndexOf('.') != -1)
            s = s.Substring(0, s.IndexOf('.'));//
        Bitmap img = Zfr.Common.Helper.GetImage("http://sh.ganji.com/common/checkcode.php?nocache=" + s, cookies, "http://sh.ganji.com/");
        if (img != null)
        {
            cd.Bitmap = img;
            //cd.Bitmap.Save("C:\\"+DateTime.Now.ToString()+".gif");
            //Show("开始->识别");
            cd.Analyse();
            c = cd.AnalysedCodesString;
            //Show("识别->验证码:" + c);
        }
        return c;
    }

}
public class CodeDiscriminator:IDisposable
{
    // Fields
    private static bool __debug = false;
    public static Color BackgroundColor = Color.White;
    private Bitmap bitmap;
    private Dictionary<char, Bitmap[]> codemap;
    private AnalysedCode[] codes;
    public static int COLORCURVE = 190;
    public static int COLORDIFF = 100;
    public static Color FillColor = Color.Black;
    public static int SINGLEPOINTAREA = 4;
    #region IDisposable Members

    public void Dispose()
    {
        DisposeT();//
    }

    #endregion
    void DisposeT()
    {
        codemap.Clear();
        codemap = null;
        codes = null;
        bitmap.Dispose();
        bitmap = null;//
    }
    #region IDisposable Members

    void IDisposable.Dispose()
    {
        DisposeT();//
    }

    #endregion
    // Methods
    public CodeDiscriminator()
    {
     
    }
    static bool IsInit = false;
    public void Init()
    {
       
        char ch;
        Console.WriteLine("Init->Start");
        if (!IsInit)
        {
            this.codemap = new Dictionary<char, Bitmap[]>();
            for (ch = 'a'; ch <= 'z'; ch = (char)(ch + '\x0001'))
            {
                this.codemap.Add(ch, this.GraphicValicodes(ch, -5, 5));
            }
            for (ch = 'A'; ch <= 'Z'; ch = (char)(ch + '\x0001'))
            {
                this.codemap.Add(ch, this.GraphicValicodes(ch, -5, 5));
            }
            for (ch = '0'; ch <= '9'; ch = (char)(ch + '\x0001'))
            {
                this.codemap.Add(ch, this.GraphicValicodes(ch, -5, 5));
            }
            //ch = null;
            IsInit = true;//
        }
        Console.WriteLine("Init->End");
    }
    public bool Analyse()
    {
        if (this.Bitmap == null)
        {
            return false;
        }
        //debug(this.Bitmap, "analysing");
        Bitmap bitmap = this.DropColor2(this.Bitmap);
        //debug(bitmap, "droped");
        bitmap = this.SweepWeed(bitmap);
        //debug(bitmap, "sweeped_1");
        bitmap = this.SweepPoint(bitmap);
        //debug(bitmap, "sweeped_2");
        bitmap = this.CleanBitmap(bitmap);
        //debug(bitmap, "cleaned");
        Bitmap[] bitmapArray = this.SplitBitmap(bitmap);
        this.codes = new AnalysedCode[bitmapArray.Length];
        for (int i = 0; i < bitmapArray.Length; i++)
        {
            //debug(bitmapArray[i], "splited" + i + "xx");
            this.codes[i] = this.Analyse(bitmapArray[i]);
        }
        bitmapArray = null; bitmap = null;//zfr
        return true;
    }

    public AnalysedCode Analyse(Bitmap bitmap)
    {
        float rate = -10000f;
        char code = 'a';
        Bitmap a = null;
        int num2 = 0; Bitmap[] bitmapArray; Bitmap bitmap4;
        bool flag; bool flag2;
        foreach (char ch2 in this.codemap.Keys)
        {
            bitmapArray = this.codemap[ch2];
            foreach (Bitmap bitmap3 in bitmapArray)
            {
                num2++;
                bitmap4 = new Bitmap(bitmap3, bitmap.Size);
                int num3 = 0;
                int num4 = 0;
                for (int i = 0; i < bitmap4.Width; i++)
                {
                    for (int j = 0; j < bitmap4.Height; j++)
                    {
                        flag = false;
                        flag2 = false;
                        if (bitmap4.GetPixel(i, j).ToArgb() != BackgroundColor.ToArgb())
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                        flag2 = bitmap.GetPixel(i, j).ToArgb() != BackgroundColor.ToArgb();
                        if (flag == flag2)
                        {
                            num3++;
                        }
                        num4++;
                    }
                }
                if (num2 == 0x16b)
                {
                }
                //debug(string.Concat(new object[] { ch2, ":", (num3 * 100f) / ((float) num4), "%" }));
                if (rate < (((float) num3) / ((float) num4)))
                {
                    rate = ((float) num3) / ((float) num4);
                    code = ch2;
                    a = bitmap4;
                }
            }
        }
        //debug(string.Concat(new object[] { "Max Match Is : ", code, ":", rate * 100f, "%" }));
        //debug(this.MixBitmap(a, bitmap), "max_" + DateTime.Now.Ticks);
        bitmap4 = null; bitmapArray = null;//zfr
        return new AnalysedCode(code, rate);
    }

    private Bitmap CleanBitmap(Bitmap bmap)
    {
        return (Bitmap) this.RemoveWhite(bmap, 0, 0);
    }

    public static void debug()
    {
        if (__debug)
        {
            try
            {
                if (Directory.Exists("debug"))
                {
                    Directory.Delete("debug", true);
                }
                Directory.CreateDirectory("debug");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    public static void debug(string output)
    {
        if (__debug)
        {
            Console.WriteLine(output);
            try
            {
                Console.WriteLine(output);
                if (!File.Exists("debug/debug.txt"))
                {
                    File.Create("debug/debug.txt");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    public static void debug(Bitmap bitmap, string name)
    {
        if (__debug)
        {
            try
            {
                if (!Directory.Exists("debug"))
                {
                    Directory.CreateDirectory("debug");
                }
                bitmap.Save("debug/" + name + ".bmp", ImageFormat.Bmp);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    private Bitmap DropColor(Bitmap bitmap, Color[] colors)
    {
        Bitmap bitmap2 = new Bitmap(bitmap);
        for (int i = 0; i < bitmap2.Height; i++)
        {
            for (int j = 0; j < bitmap2.Width; j++)
            {
                bool flag = false;
                for (int k = 0; k < colors.Length; k++)
                {
                    if (bitmap2.GetPixel(j, i).ToArgb() == colors[k].ToArgb())
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    bitmap2.SetPixel(j, i, BackgroundColor);
                }
                else
                {
                    bitmap2.SetPixel(j, i, FillColor);
                }
            }
        }
        return bitmap2;
    }

    private Bitmap DropColor2(Bitmap bitmap)
    {
        Bitmap bitmap2 = new Bitmap(bitmap); Color pixel; bool flag;
        for (int i = 0; i < bitmap2.Height; i++)
        {
            for (int j = 0; j < bitmap2.Width; j++)
            {
                flag = false;
                pixel = bitmap2.GetPixel(j, i);
                if (((pixel.R >= COLORCURVE) && (pixel.G >= COLORCURVE)) && (pixel.B >= COLORCURVE))
                {
                    flag = false;
                }
                else
                {
                    flag = true;
                }
                if (!flag)
                {
                    bitmap2.SetPixel(j, i, BackgroundColor);
                }
            }
        }
        return bitmap2;
    }

    private Color[] GetColor(Bitmap bitmap)
    {
        Color[] color = this.GetColor(bitmap, bitmap.Height / 4);
        Color[] colorArray2 = this.GetColor(bitmap, (bitmap.Height * 2) / 4);
        Color[] colorArray3 = this.GetColor(bitmap, (bitmap.Height * 3) / 4);
        Color[] array = new Color[(color.Length + colorArray2.Length) + colorArray3.Length];
        color.CopyTo(array, 0);
        colorArray2.CopyTo(array, color.Length);
        colorArray3.CopyTo(array, (int) (color.Length + colorArray2.Length));
        return array;
    }

    private Color[] GetColor(Bitmap bitmap, int height)
    {
        List<Color> list = new List<Color>();
        int num = 2;
        int y = height;
        int x = -1; Color pixel; bool flag;
        while (true)
        {
            
            do
            {
                if (++x >= bitmap.Width)
                {
                    return list.ToArray();
                }
                pixel = bitmap.GetPixel(x, y);
            }
            while ((pixel.ToArgb() == BackgroundColor.ToArgb()) || ((x + num) >= bitmap.Width));
            flag= true;
            for (int i = x; i < (x + num); i++)
            {
                if (bitmap.GetPixel(i, y).ToArgb() != pixel.ToArgb())
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                list.Add(pixel);
                x += num;
            }
        }
    }

    public Bitmap GraphicValicode(char c, int degree)
    {
        Bitmap image = new Bitmap(100, 100, PixelFormat.Format48bppRgb);
        Graphics graphics = Graphics.FromImage(image);
        Font font = new Font("Arial", 26f, FontStyle.Regular);
        graphics.FillRectangle(Brushes.White, 0, 0, 100, 100);
        graphics.RotateTransform((float) degree);
        graphics.DrawString(c.ToString(), font, Brushes.Black, (float) 10f, (float) 10f);
        graphics.Dispose(); graphics = null;//zfr
        return this.CleanBitmap(image);
    }

    public Bitmap[] GraphicValicodes(char c, int degree, int enddegree)
    {
        List<Bitmap> list = new List<Bitmap>();
        for (int i = degree; i <= enddegree; i += 2)
        {
            list.Add(this.GraphicValicode(c, i));
        }
        return list.ToArray();
    }

    private bool IsSimilar(Color a, Color b)
    {
        if ((Math.Max(Math.Max((int) (a.R - b.R), (int) (a.G - b.G)), a.B - b.B) + Math.Max(Math.Max((int) (b.R - a.R), (int) (b.G - a.G)), b.B - a.B)) >= COLORDIFF)
        {
            return false;
        }
        if (((a.ToArgb() == BackgroundColor.ToArgb()) && (b.ToArgb() != BackgroundColor.ToArgb())) || ((a.ToArgb() != BackgroundColor.ToArgb()) && (b.ToArgb() == BackgroundColor.ToArgb())))
        {
            return false;
        }
        return true;
    }

    private bool IsThePoint(Bitmap bmap, int x, int y)
    {
        int num3;
        int num4;
        int num = 2;
        int num2 = 2;
        Color pixel = bmap.GetPixel(x, y);
        if (pixel.ToArgb() == BackgroundColor.ToArgb())
        {
            return false;
        }
        bool flag = true;
        for (num3 = (x - num) + 1; (num3 <= x) && flag; num3++)
        {
            num4 = (y - num2) + 1;
            while ((num4 <= y) && flag)
            {
                if ((((num3 < 0) || (num3 >= bmap.Width)) || (num4 < 0)) || (num4 >= bmap.Height))
                {
                    flag = false;
                }
                else if (!this.IsSimilar(pixel, bmap.GetPixel(num3, num4)))
                {
                    flag = false;
                }
                num4++;
            }
        }
        if (flag)
        {
            return true;
        }
        flag = true;
        for (num3 = (x - num) + 1; (num3 <= x) && flag; num3++)
        {
            num4 = y;
            while ((num4 <= ((y + num2) - 1)) && flag)
            {
                if ((((num3 < 0) || (num3 >= bmap.Width)) || (num4 < 0)) || (num4 >= bmap.Height))
                {
                    flag = false;
                }
                else if (!this.IsSimilar(pixel, bmap.GetPixel(num3, num4)))
                {
                    flag = false;
                }
                num4++;
            }
        }
        if (flag)
        {
            return true;
        }
        flag = true;
        for (num3 = x; (num3 <= ((x + num) - 1)) && flag; num3++)
        {
            num4 = (y - num2) + 1;
            while ((num4 <= y) && flag)
            {
                if ((((num3 < 0) || (num3 >= bmap.Width)) || (num4 < 0)) || (num4 >= bmap.Height))
                {
                    flag = false;
                }
                else if (!this.IsSimilar(pixel, bmap.GetPixel(num3, num4)))
                {
                    flag = false;
                }
                num4++;
            }
        }
        if (flag)
        {
            return true;
        }
        flag = true;
        for (num3 = x; (num3 <= ((x + num) - 1)) && flag; num3++)
        {
            for (num4 = y; (num4 <= ((y + num2) - 1)) && flag; num4++)
            {
                if ((((num3 < 0) || (num3 >= bmap.Width)) || (num4 < 0)) || (num4 >= bmap.Height))
                {
                    flag = false;
                }
                else if (!this.IsSimilar(pixel, bmap.GetPixel(num3, num4)))
                {
                    flag = false;
                }
            }
        }
        return flag;
    }

    private int IsTheSinglePoint(Bitmap bmap, int x, int y)
    {
        int sINGLEPOINTAREA = SINGLEPOINTAREA;
        if (bmap.GetPixel(x, y).ToArgb() != BackgroundColor.ToArgb())
        {
            for (int i = 1; i <= sINGLEPOINTAREA; i++)
            {
                bool flag = true;
                int num3 = x - 1;
                while (num3 <= (x + i))
                {
                    if ((num3 >= 0) && (num3 < bmap.Width))
                    {
                        if (((y - 1) >= 0) && (bmap.GetPixel(num3, y - 1).ToArgb() != BackgroundColor.ToArgb()))
                        {
                            flag = false;
                        }
                        if (((y + i) < bmap.Height) && (bmap.GetPixel(num3, y + i).ToArgb() != BackgroundColor.ToArgb()))
                        {
                            flag = false;
                        }
                    }
                    num3++;
                }
                if (flag)
                {
                    for (num3 = y - 1; num3 <= (y + i); num3++)
                    {
                        if ((num3 >= 0) && (num3 < bmap.Height))
                        {
                            if (((x - 1) >= 0) && (bmap.GetPixel(x - 1, num3).ToArgb() != BackgroundColor.ToArgb()))
                            {
                                flag = false;
                            }
                            if (((x + i) < bmap.Width) && (bmap.GetPixel(x + i, num3).ToArgb() != BackgroundColor.ToArgb()))
                            {
                                flag = false;
                            }
                        }
                    }
                    if (flag)
                    {
                        return i;
                    }
                }
            }
        }
        return -1;
    }

    private Bitmap MixBitmap(Bitmap a, Bitmap b)
    {
        Bitmap bitmap = new Bitmap(a);
        for (int i = 0; i < bitmap.Width; i++)
        {
            for (int j = 0; j < bitmap.Height; j++)
            {
                if (b.GetPixel(i, j).ToArgb() != BackgroundColor.ToArgb())
                {
                    bitmap.SetPixel(i, j, Color.Red);
                }
            }
        }
        return bitmap;
    }

    private Image RemoveWhite(Image source, int w_white, int h_white)
    {
        try
        {
            int num9;
            int num = w_white;
            int num2 = h_white;
            Bitmap bitmap = new Bitmap(source);
            int height = bitmap.Height;
            int width = bitmap.Width;
            int y = 0;
            int num6 = height - 1;
            int x = 0;
            int num8 = width - 1;
            while (y < num6)
            {
                num9 = 0;
                while (num9 < width)
                {
                    if (bitmap.GetPixel(num9, y).ToArgb() != Color.White.ToArgb())
                    {
                        goto Label_00D7;
                    }
                    num9++;
                }
                y++;
            }
        Label_00D7:
            while (num6 > 0)
            {
                num9 = 0;
                while (num9 < width)
                {
                    if (bitmap.GetPixel(num9, num6).ToArgb() != Color.White.ToArgb())
                    {
                        goto Label_0130;
                    }
                    num9++;
                }
                num6--;
            }
        Label_0130:
            while (x < width)
            {
                num9 = 0;
                while (num9 < height)
                {
                    if (bitmap.GetPixel(x, num9).ToArgb() != Color.White.ToArgb())
                    {
                        goto Label_018A;
                    }
                    num9++;
                }
                x++;
            }
        Label_018A:
            while (num8 > 0)
            {
                for (num9 = 0; num9 < height; num9++)
                {
                    if (bitmap.GetPixel(num8, num9).ToArgb() != Color.White.ToArgb())
                    {
                        goto Label_0195;
                    }
                }
                num8--;
            }
        Label_0195:
            if ((((((num * 2) + num8) - x) + 1) <= 0) || (((((num2 * 2) + num6) - y) + 1) < 0))
            {
                return new Bitmap((num * 2) + 1, (num2 * 2) + 1, PixelFormat.Format32bppRgb);
            }
            Bitmap bitmap2 = new Bitmap((((num * 2) + num8) - x) + 1, (((num2 * 2) + num6) - y) + 1, PixelFormat.Format32bppRgb);
            for (int i = 0; i < bitmap2.Width; i++)
            {
                for (int j = 0; j < bitmap2.Height; j++)
                {
                    if ((((i <= (num - 1)) || (i >= (bitmap2.Width - num))) || (j <= (num2 - 1))) || (j >= (bitmap2.Height - num2)))
                    {
                        bitmap2.SetPixel(i, j, Color.White);
                    }
                    else
                    {
                        bitmap2.SetPixel(i, j, bitmap.GetPixel((i - num) + x, (j - num2) + y));
                    }
                }
            }
            return bitmap2;
        }
        catch (Exception ex)
        {
            throw ex;
            return source;
        }
    }

    private Bitmap[] SplitBitmap(Bitmap bmap)
    {
        List<Bitmap> list = new List<Bitmap>();
        int num = 0;
        bool flag = true;
        for (int i = 0; i < (bmap.Width + 1); i++)
        {
            bool flag2 = true;
            if (i != bmap.Width)
            {
                for (int j = 0; j < bmap.Height; j++)
                {
                    if (((bmap.GetPixel(i, j).ToArgb() != BackgroundColor.ToArgb()) && ((j + 1) < bmap.Height)) && (bmap.GetPixel(i, j + 1).ToArgb() != BackgroundColor.ToArgb()))
                    {
                        flag2 = false;
                        flag = false;
                        break;
                    }
                }
            }
            if ((flag2 && (i > num)) && !flag)
            {
                Bitmap bitmap = new Bitmap((i - num) - 1, bmap.Height);
                for (int k = num + 1; k < i; k++)
                {
                    for (int m = 0; m < bmap.Height; m++)
                    {
                        bitmap.SetPixel((k - num) - 1, m, bmap.GetPixel(k, m));
                    }
                }
                bitmap = this.CleanBitmap(bitmap);
                if (((bitmap.Width > 2) && (bitmap.Height > 2)) && ((bitmap.Width > 5) || (bitmap.Height > 5)))
                {
                    list.Add(bitmap);
                }
                num = i;
                flag = true; //bitmap.Dispose(); bitmap = null;//zfr
            }
        }
        return list.ToArray();
    }

    private Bitmap SweepPoint(Bitmap bmap)
    {
        Bitmap bitmap = new Bitmap(bmap);
        for (int i = 0; i < bitmap.Width; i++)
        {
            for (int j = 0; j < bitmap.Height; j++)
            {
                int num3 = this.IsTheSinglePoint(bitmap, i, j);
                if (num3 != -1)
                {
                    for (int k = 0; k < num3; k++)
                    {
                        for (int m = 0; m < num3; m++)
                        {
                            if (((i + k) < bitmap.Width) && ((j + m) < bitmap.Height))
                            {
                                bitmap.SetPixel(i + k, j + m, BackgroundColor);
                            }
                        }
                    }
                }
            }
        }
        return bitmap;
    }

    private Bitmap SweepWeed(Bitmap bmap)
    {
        Bitmap bitmap = new Bitmap(bmap);
        for (int i = 0; i < bitmap.Width; i++)
        {
            for (int j = 0; j < bitmap.Height; j++)
            {
                if (!this.IsThePoint(bitmap, i, j))
                {
                    bitmap.SetPixel(i, j, BackgroundColor);
                }
            }
        }
        return bitmap;
    }

    // Properties
    public AnalysedCode[] AnalysedCodes
    {
        get
        {
            return this.codes;
        }
    }

    public string AnalysedCodesString
    {
        get
        {
            string str = "";
            if (this.codes != null)
            {
                foreach (AnalysedCode code in this.codes)
                {
                    str = str + code.code;
                }
            }
            return str;
        }
    }

    public Bitmap Bitmap
    {
        get
        {
            return this.bitmap;
        }
        set
        {
            this.bitmap = new Bitmap(value);
        }
    }

    public static bool Debug
    {
        get
        {
            return __debug;
        }
        set
        {
            __debug = value;
            debug();
        }
    }

    public static float Version
    {
        get
        {
            return 1.1f;
        }
    }

    // Nested Types
    public class AnalysedCode:IDisposable
    {
        // Fields
        public char code;
        public float rate;

        // Methods
        public AnalysedCode(char code, float rate)
        {
            this.code = code;
            this.rate = rate;
        }

        #region IDisposable Members

        public void Dispose()
        {
           // code = Char.;
        }

        #endregion

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            //code = null;
        }

        #endregion
    }

    
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页