2013-09-24 104 views
-1

我試圖顯示一個窗體,顯示一個標籤,如「更新,此窗口將關閉一旦更新完成」下載一些圖像文件。我把這個放在我的表格上。C# - 下載並保存圖像到文件夾

private void frmExtraUpdater_Shown(object sender, EventArgs e) 
    { 

     for (int i = 1; i < 8; i++) 
     { 
      string _EmoticonURL = String.Format("https://dl.dropboxusercontent.com/u/110636189/MapleEmoticons/f{0}.bmp", i); 

      WebRequest requestPic = WebRequest.Create(_EmoticonURL); 

      WebResponse responsePic = requestPic.GetResponse(); 

      Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error 

      webImage.Save(Application.StartupPath + @"\Images\f" + i + ".bmp"); 
     } 
    } 

但是..一旦所示的形式,標籤甚至不顯示,因爲它不會加載它(這insantly downloades的圖像。我想讓它顯示的標籤,然後纔開始下載)。

另一個問題是它會拋出「在GDI +中發生的一般錯誤。」在webImgae.Save部分出於某種原因。

這是爲什麼?

哦,如果文件夾「Images」不存在,它會自動創建它嗎?

謝謝!

+0

你爲什麼不只是使用'WebClient.DownloadFile'將文件保存直盤?不,該目錄不會自動創建。 –

回答

1

這裏是喬恩斯基特建議:

System.Net.WebClient webClient = new System.Net.WebClient(); 
webClient.DownloadFile(String.Format("https://dl.dropboxusercontent.com/u/110636189/MapleEmoticons/f{0}.bmp", i), Application.StartupPath + @"\Images\f" + i + ".bmp"); 

使用後臺工作,以更新任何UI組件

BackgroundWorker backTask = new BackgroundWorker(); 
    public frmExtraUpdater() 
    { 
     backTask.DoWork += backTask_DoWork; 
     backTask.RunWorkerCompleted += backTask_RunWorkerCompleted; 
    } 
    private void frmExtraUpdater_Shown(object sender, EventArgs e) 
    { 
     yourLabel.Text = "Downloading"; 
     backTask.RunWorkerAsync(); 
    } 

    void backTask_DoWork(object sender, DoWorkEventArgs e) 
    { 
     for (int i = 1; i < 8; i++) 
     { 
      System.Net.WebClient webClient = new System.Net.WebClient(); 
      webClient.DownloadFile(String.Format("https://dl.dropboxusercontent.com/u/110636189/MapleEmoticons/f{0}.bmp", i), Application.StartupPath + @"\Images\f" + i + ".bmp"); 
      backTask.ReportProgress(i * (100/8), String.Format("https://dl.dropboxusercontent.com/u/110636189/MapleEmoticons/f{0}.bmp", i)); 
     } 
    } 

    void backTask_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     yourLabel.Text = "Downloading" + e.UserState.ToString(); 
    } 

    void backTask_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     this.Close(); 
    } 
+0

謝謝。標籤問題呢?該標籤似乎全部是白色背景,並且不直接顯示和下載。 – user2810715

+0

出於某種原因它說「在WebClient請求期間發生異常。」。我的保存/下載URL很好。我嘗試在每次下載之間放置Thread.Sleep(3000),但仍然失敗。 – user2810715

+0

檢查您的例外情況:它可能是代理身份驗證要求,您的下載目錄中的ACL,網站身份驗證 – Guish

0

您應該使用異步方法,用WebRequest的異步API(如果你一起工作。淨4.5或sup)或在後臺線程中下載您的圖像,以避免ui線程阻塞。

你可以試試:

private void frmExtraUpdater_Shown(object sender, EventArgs e) 
    { 
     BackgroundWorker worker = new BackgroundWorker(); 

     worker.DoWork += (o, d) => 
     { 
      saveImages(); 
     }; 
     worker.RunWorkerCompleted += (o, f) => 
     { 
      this.Close(); 
     }; 
     worker.RunWorkerAsync(); 
    } 

    private void saveImages() 
    { 
     for (int i = 1; i < 8; i++) 
     { 
      string _EmoticonURL = String.Format("https://dl.dropboxusercontent.com/u/110636189/MapleEmoticons/f{0}.bmp", i); 

      WebRequest requestPic = WebRequest.Create(_EmoticonURL); 

      WebResponse responsePic = requestPic.GetResponse(); 

      Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error 

      webImage.Save(Application.StartupPath + @"\Images\f" + i + ".bmp"); 
     } 
    } 
相關問題