Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog DigOpen = new OpenFileDialog();

            DigOpen.ShowDialog();
            string Path = DigOpen.FileName;

            if (Path.Length == 0)
            {
                MessageBox.Show("Please select Txt file");
                return;
            }

            //     MessageBox.Show(Path);

            //downloading image from urls

            WebClient wc = new WebClient();// key member of downloading

            Thunder_SDK.XLInitDownloadEngine();

            MemoryStream ms;
            StreamReader m_StreamReader = new StreamReader(Path);// open the txt file with C# form

            while (true)
            {
                String temp_line = m_StreamReader.ReadLine();// read one line of txt file 用的挺爽
                if (temp_line == null)
                {
                    break;                              // break when reach the end of file;
                }
                String[] parts = temp_line.Split('\t'); // split the readed line by '\t'
                if ((parts[0].IndexOf('#')) != -1)
                {
                    continue;// the first and second  row of image is useless so discard them
                }

                String ImgSavePath = "Y:\\GitHub\\Test_saveimg\\Test_saveimg\\Test_saveimg\\bin\\Release\\" + parts[0] + "\\";
                if (!Directory.Exists(ImgSavePath))
                {
                    Directory.CreateDirectory(ImgSavePath);// create the Directiory when necessary
                }


                ImgSavePath += parts[0] + "_" + parts[1] + ".jpg";

                // replace the download by xunlei engine
                String filename = parts[0] + "_" + parts[1] + ".jpg";


                label1.Text = parts[0] + "_" + parts[1] + ".jpg";
                //    label1.Font = new System.Drawing.Font()
                this.Refresh();


                MyObj_para para = new MyObj_para();

                para.ImgSavePath = ImgSavePath;
                para.parts2      = parts[2];
                para.temp_line   = temp_line;
                para.label       = label2;
                para.progressbar = progressBar1;



                Int32 lTaskId = 0;
                Int32 dwRet   = Thunder_SDK.XLURLDownloadToFile(ImgSavePath, parts[2], "", ref lTaskId);
                if (Thunder_SDK.XL_SUCCESS != dwRet)
                {
                    //ThunderSDK.Program.XLUninitDownloadEngine();
                    //      MessageBox.Show (string.Format("Create new task failed, error code:%d\n", dwRet));
                }
                float last = 0;
                do
                {
                    Thread.Sleep(500);
                    last += 0.5F;



                    Int64 ullFileSize = 0;
                    Int64 ullRecvSize = 0;
                    int   lStatus     = -1;

                    dwRet = Thunder_SDK.XLQueryTaskInfo(lTaskId, ref lStatus, ref ullFileSize, ref ullRecvSize);
                    if (Thunder_SDK.XL_SUCCESS == dwRet)
                    {
                        // 输出进度信息
                        if (0 != ullFileSize)
                        {
                            double douProgress = (double)ullRecvSize / (double)ullFileSize;
                            douProgress *= 100.0;

                            progressBar1.Step = (Int32)douProgress;
                            progressBar1.PerformStep();
                        }
                        else
                        {
                        }

                        if (last > 5 && 0 == ullFileSize)
                        {
                            lStatus = 12;
                        }

                        if (11 == lStatus)
                        {
                            //   MessageBox.Show(string.Format("Download successfully.\n"));
                            progressBar1.Value = 0;
                            // this.Refresh();
                            break;
                        }

                        if (12 == lStatus)
                        {
                            //  MessageBox.Show(string.Format("Download failed.\n"));
                            progressBar1.Value = 0;
                            //  this.Refresh();

                            StreamWriter m_StreamWriter = new StreamWriter(".\\dev_urls_failed.txt", true);
                            m_StreamWriter.Write(temp_line);
                            m_StreamWriter.Write("\n");
                            m_StreamWriter.Close();

                            break;
                        }
                        label2.Text = string.Format("Last {0} Sec!", last);
                    }

                    //   System.Delegate a = new System.Delegate()
                    //  Control.Invoke(500, null, null);
                    //   this.Refresh();
                    Thread thread = new Thread(new ParameterizedThreadStart(Draw));
                    thread.Start(para);
                    thread.Join();
                } while (Thunder_SDK.XL_SUCCESS == dwRet);
                Thunder_SDK.XLStopTask(lTaskId);//搜索程序



                //                  while(true)
                //                  {
                //                       String status = agent.GetTaskInfo(parts[2],"Status");
                //                      if ((string.Compare(status,"failed"))==0||(string.Compare(status,"success"))==0)
                //                      {
                //                          break;
                //                      }
                //                  }

                //                  ImgSavePath += parts[0] + "_" + parts[1] + ".jpg";// path of saving image
                //
                //                  try
                //                  {
                //                      wc.DownloadFile(parts[2], ImgSavePath); // using the DownloadFile function instead of DownloadData
                //                //      ms = new MemoryStream(wc.DownloadData(parts[2]));
                //                //      Image img = Image.FromStream(ms);
                //                //      img.Save(ImgSavePath);
                //                  }
                //                  catch (System.Exception ex)
                //                  {
                //                   //    MessageBox.Show(ex.Message + parts[2]);// catch the exception when the image is invalid
                //
                //                  }
            }

            Thunder_SDK.XLUninitDownloadEngine();



            //      MessageBox.Show("Debug Here!!");
            //       WebClient wc = new WebClient();
            //     string shitu_url = "http://stu.baidu.com/i?objurl=http://www.hinews.cn/pic/0/16/31/87/16318729_751468.jpg&rt=0&rn=10&ftn=searchstu&ct=1&stt=1&tn=infacejson";
            //    shitu_url += "pn=0";


            //    http://stu.baidu.com/i?ct=3&tn=facejson&pn=0&rn=10&querysign=1267449575,1337723570&shituRetNum=586&similarRetNum=600&faceRetNum=1000&setnum=0
            //     byte[] aryByte = wc.DownloadData(shitu_url);
            //     ms = new MemoryStream(aryByte);
            //     string html = string.Empty;
            //     using (StreamReader sr = new StreamReader(ms, Encoding.Default))
            //     {
            //         html = sr.ReadToEnd();
            //     }

            // baidujason jsonObj = JArray.Parse(html);

            //     baidujason ja = (baidujason)JsonConvert.DeserializeObject(html, typeof(baidujason));


            System.Diagnostics.Process.Start(@"http://blog.csdn.net/likely_zhao/article/details/38424959");
        }
Пример #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog DigOpen = new OpenFileDialog();
            DigOpen.ShowDialog();            
            string Path = DigOpen.FileName;
            if (Path.Length == 0)
            {
                MessageBox.Show("Please select Txt file");
                return;
            }

       //     MessageBox.Show(Path);

            //downloading image from urls

            WebClient wc = new WebClient();// key member of downloading
            Thunder_SDK.XLInitDownloadEngine();        

            MemoryStream ms;
            StreamReader m_StreamReader = new StreamReader(Path);// open the txt file with C# form     

            while (true)
            {
                String temp_line = m_StreamReader.ReadLine();// read one line of txt file 用的挺爽
                if (temp_line == null)
                {
                    break;// break when reach the end of file;
                }
                String[] parts = temp_line.Split('\t');// split the readed line by '\t'
                if ((parts[0].IndexOf('#')) != -1)
                {
                    continue;// the first and second  row of image is useless so discard them

                }

                String ImgSavePath = "Y:\\GitHub\\Test_saveimg\\Test_saveimg\\Test_saveimg\\bin\\Release\\" + parts[0] + "\\";
                if (!Directory.Exists(ImgSavePath))
                {
                    Directory.CreateDirectory(ImgSavePath);// create the Directiory when necessary 
                }


                ImgSavePath += parts[0] + "_" + parts[1] + ".jpg";

                // replace the download by xunlei engine
                String filename = parts[0] + "_" + parts[1] + ".jpg";
       
             
                label1.Text = parts[0] + "_" + parts[1] + ".jpg";
            //    label1.Font = new System.Drawing.Font()
                this.Refresh();


                MyObj_para para = new MyObj_para();

                para.ImgSavePath = ImgSavePath;
                para.parts2 = parts[2];
                para.temp_line = temp_line;
                para.label = label2;
                para.progressbar = progressBar1;

                
          

                Int32 lTaskId = 0;
                Int32 dwRet = Thunder_SDK.XLURLDownloadToFile(ImgSavePath, parts[2], "", ref lTaskId);
                if (Thunder_SDK.XL_SUCCESS != dwRet)
                {
                    //ThunderSDK.Program.XLUninitDownloadEngine();
                    //      MessageBox.Show (string.Format("Create new task failed, error code:%d\n", dwRet));                     
                }
                float last = 0;
                do
                {
                    Thread.Sleep(500);
                    last += 0.5F;



                    Int64 ullFileSize = 0;
                    Int64 ullRecvSize = 0;
                    int lStatus = -1;

                    dwRet = Thunder_SDK.XLQueryTaskInfo(lTaskId, ref lStatus, ref ullFileSize, ref ullRecvSize);
                    if (Thunder_SDK.XL_SUCCESS == dwRet)
                    {
                        // 输出进度信息
                        if (0 != ullFileSize)
                        {
                            double douProgress = (double)ullRecvSize / (double)ullFileSize;
                            douProgress *= 100.0;

                            progressBar1.Step = (Int32)douProgress;
                            progressBar1.PerformStep();
                        }
                        else
                        {

                        }

                        if (last > 5 && 0 == ullFileSize)
                        {
                            lStatus = 12;
                        }

                        if (11 == lStatus)
                        {
                            //   MessageBox.Show(string.Format("Download successfully.\n"));
                            progressBar1.Value = 0;
                            // this.Refresh();
                            break;
                        }

                        if (12 == lStatus)
                        {
                            //  MessageBox.Show(string.Format("Download failed.\n"));
                            progressBar1.Value = 0;
                            //  this.Refresh();

                            StreamWriter m_StreamWriter = new StreamWriter(".\\dev_urls_failed.txt", true);
                            m_StreamWriter.Write(temp_line);
                            m_StreamWriter.Write("\n");
                            m_StreamWriter.Close();

                            break;
                        }
                        label2.Text = string.Format("Last {0} Sec!", last);

                    }

                 //   System.Delegate a = new System.Delegate()
                  //  Control.Invoke(500, null, null);
                 //   this.Refresh();
                    Thread thread = new Thread(new ParameterizedThreadStart(Draw));
                    thread.Start(para); 
                    thread.Join();

                } while (Thunder_SDK.XL_SUCCESS == dwRet);
                Thunder_SDK.XLStopTask(lTaskId);//搜索程序
     
        


       
             

          

    //                  while(true)
    //                  {
    //                       String status = agent.GetTaskInfo(parts[2],"Status");
    //                      if ((string.Compare(status,"failed"))==0||(string.Compare(status,"success"))==0)
    //                      {
    //                          break;
    //                      }
    //                  }

    //                  ImgSavePath += parts[0] + "_" + parts[1] + ".jpg";// path of saving image
    // 
    //                  try
    //                  {
    //                      wc.DownloadFile(parts[2], ImgSavePath); // using the DownloadFile function instead of DownloadData
    //                //      ms = new MemoryStream(wc.DownloadData(parts[2]));
    //                //      Image img = Image.FromStream(ms);
    //                //      img.Save(ImgSavePath); 
    //                  }
    //                  catch (System.Exception ex)
    //                  {
    //                   //    MessageBox.Show(ex.Message + parts[2]);// catch the exception when the image is invalid
    // 
    //                  }



            }

            Thunder_SDK.XLUninitDownloadEngine();





            //      MessageBox.Show("Debug Here!!");
            //       WebClient wc = new WebClient();
       //     string shitu_url = "http://stu.baidu.com/i?objurl=http://www.hinews.cn/pic/0/16/31/87/16318729_751468.jpg&rt=0&rn=10&ftn=searchstu&ct=1&stt=1&tn=infacejson";
            //    shitu_url += "pn=0";


            //    http://stu.baidu.com/i?ct=3&tn=facejson&pn=0&rn=10&querysign=1267449575,1337723570&shituRetNum=586&similarRetNum=600&faceRetNum=1000&setnum=0
       //     byte[] aryByte = wc.DownloadData(shitu_url);
       //     ms = new MemoryStream(aryByte);
       //     string html = string.Empty;
       //     using (StreamReader sr = new StreamReader(ms, Encoding.Default))
       //     {
       //         html = sr.ReadToEnd();
       //     }

            // baidujason jsonObj = JArray.Parse(html);

       //     baidujason ja = (baidujason)JsonConvert.DeserializeObject(html, typeof(baidujason));


            System.Diagnostics.Process.Start(@"http://blog.csdn.net/likely_zhao/article/details/38424959");
        }