-1

我正在尝试显示一个显示“更新,更新完成后此窗口将关闭”之类的标签的表单,以下载一些图像文件。我把这个放在我的表格上。

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");
        }
    }

但是..一旦显示表单,标签甚至不会显示,因为它没有加载它(它会立即下载图像。我希望它显示标签,然后才开始下载)。

另一个问题是它抛出“GDI+ 中发生一般错误”。在 webImgae.Save 部分出于某种原因。

为什么?

哦,如果文件夹“Images”不存在,它会自动创建它吗?

谢谢!

4

2 回答 2

1

这是 Jon Skeet 的建议:

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(); 
    }
于 2013-09-24T14:56:57.143 回答
0

您应该使用 async 方法和 webrequest async api(如果您使用 .net 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");
        }
    } 
于 2013-09-24T15:23:16.733 回答