Exemple #1
0
        public static List <PicCategory> GetCategory(string sPath, int iMaxCategory)
        {
            List <PicCategory> listCategory = new List <PicCategory>();

            string sUrl = "http://www.coloring-book.info/coloring/coloring_page.php?id=";

            for (int i = 1; i <= iMaxCategory; i++)
            {
                string sRealUrl = sUrl + i.ToString();

                WebClient wc    = new WebClient();
                string    sHtml = wc.DownloadString(sRealUrl);

                Regex reg = new Regex("<td width=\"95\"><img.*?src=\"(.*?)\".*?></td>.*?<td width=\"468\"><div align=\"center\"><h1 class=\"coloring\">(.*?) coloring pages</h1>", RegexOptions.Singleline);

                Match m = reg.Match(sHtml);

                PicCategory c = new PicCategory();
                c.ID          = i;
                c.CategoryPic = @"http://www.coloring-book.info/coloring/" + m.Groups[1].Value;
                c.Name        = m.Groups[2].Value.Replace(":", "");
                if (c.Name != "")
                {
                    listCategory.Add(c);
                }
            }

            return(listCategory);
        }
        private void btnCollectDirectory_Click(object sender, EventArgs e)
        {
            //Get the DictFile
            string[] reads = File.ReadAllLines(tbPicPath.Text + "\\Translate.txt");
            Dictionary <string, string> mapName = new Dictionary <string, string>();

            foreach (string r in reads)
            {
                string[] ss = r.Split('|');
                mapName[ss[0]] = ss[1];
            }

            DirectoryInfo info = new DirectoryInfo(tbPicPath.Text + "_processed");

            DirectoryInfo[] dirs = info.GetDirectories();
            Dictionary <int, PicCategory> mapDir = new Dictionary <int, PicCategory>();

            foreach (DirectoryInfo inf in dirs)
            {
                string[] ss = inf.Name.Split('_');

                PicCategory c = new PicCategory();
                c.ID     = Int32.Parse(ss[1]);
                c.SortID = Int32.Parse(ss[2]);
                c.EName  = ss[0];
                if (mapName.ContainsKey(ss[0]))
                {
                    c.CName = mapName[ss[0]];
                }
                else
                {
                    c.CName = c.EName;
                }
                c.SrcPath        = inf.FullName;
                mapDir[c.SortID] = c;
            }

            string sNewPath = tbPicPath.Text + "_Output";

            Directory.CreateDirectory(sNewPath);

            string sJson = "[";

            //Change the order!
            int iMaxSortID = mapDir.Count;


            for (int iCur = 0; iCur < iMaxSortID; iCur++)
            {
                PicCategory c        = mapDir[iCur];
                string      sCatPath = sNewPath + "\\" + c.ID;
                Directory.CreateDirectory(sCatPath);

                FileInfo[] fileNames = new DirectoryInfo(c.SrcPath).GetFiles();
                int        iCnt      = (fileNames.Length - 1) / 2;

                sJson += "{c:" + c.ID + ",n:'" + c.CName.Replace("\'", "\\\'") + "',max:" + iCnt.ToString() + "},";

                foreach (FileInfo fInfo in fileNames)
                {
                    if (fInfo.Name == "category.gif")
                    {
                        Bitmap bm = Bitmap.FromFile(fInfo.FullName) as Bitmap;
                        bm.Save(sCatPath + "\\category.png", ImageFormat.Png);
                    }
                    else
                    {
                        fInfo.CopyTo(sCatPath + "\\" + fInfo.Name);
                    }
                }
            }

            sJson  = sJson.Substring(0, sJson.Length - 1);
            sJson += "]";

            File.WriteAllText(sNewPath + "\\Category.txt", sJson);
        }