每次下载文件时如何更新pictureBox中的图像?

时间:2016-12-29 19:11:22

标签: c# .net winforms

    public partial class Form1 : Form
    {
        WebClient webClient;               // Our WebClient that will be doing the downloading for us
        Stopwatch sw = new Stopwatch();    // The stopwatch which we will be using to calculate the download speed
        int count = 0;
        PictureBoxBigSize pbbs;

        public Form1()
        {
            ...
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnDownload_Click(object sender, EventArgs e)
        {      
                DownloadFile(ExtractImages.imagesUrls[count], @"C:\Temp\TestingSatelliteImagesDownload\" + count + ".jpg");
        }

        public void DownloadFile(string urlAddress, string location)
        {
            using (webClient = new WebClient())
            {
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

                // The variable that will be holding the url address (making sure it starts with http://)
                Uri URL = urlAddress.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ? new Uri(urlAddress) : new Uri("http://" + urlAddress);

                // Start the stopwatch which we will be using to calculate the download speed
                sw.Start();
                txtFileName.Text = count + ".jpg";
                try
                {
                    // Start downloading the file
                    webClient.DownloadFileAsync(URL, location);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        // The event that will fire whenever the progress of the WebClient is changed
        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            // Calculate download speed and output it to labelSpeed.
            Label2.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));

            // Update the progressbar percentage only when the value is not the same.
            ProgressBar1.Value = e.ProgressPercentage;

            // Show the percentage on our label.
            Label4.Text = e.ProgressPercentage.ToString() + "%";

            // Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
            Label5.Text = string.Format("{0} MB's / {1} MB's",
                (e.BytesReceived / 1024d / 1024d).ToString("0.00"),
                (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
        }

        // The event that will trigger when the WebClient is completed
        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            // Reset the stopwatch.
            sw.Reset();

            if (e.Cancelled == true)
            {
                MessageBox.Show("Download has been canceled.");
            }
            else
            {
                pictureBox1.Image = new Bitmap(@"C:\Temp\TestingSatelliteImagesDownload\" + count + ".jpg");
                pictureBox1.Update();
                count++;
                DownloadFile(ExtractImages.imagesUrls[count], @"C:\Temp\TestingSatelliteImagesDownload\" + count + ".jpg");
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            // 845, 615
            PictureBoxBigSize.pb.Image = pictureBox1.Image;
            pbbs = new PictureBoxBigSize();
            pbbs.Show();
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pbbs.Close();
        }
    }

在完成的活动中,我添加了这两行:

pictureBox1.Image = new Bitmap(@"C:\Temp\TestingSatelliteImagesDownload\" + count + ".jpg");
pictureBox1.Update();

有时它会更新pictureBox并在下载每个图像后立即显示图像。所以它就像动画一样。

但有时在大多数情况下,它不是每隔X秒显示图像。 我尝试使用pictureBox1.Refresh和Invalidate,现在更新。 我试图添加Thread.Sleep(40);或睡觉(500),但到目前为止,它一直没有工作。

我还创建了一个新的表单,所以当我在pictureBox1区域内移动鼠标时,它会以更大的尺寸显示新的表单和pictureBox我也希望以新的形式显示图像。但与form1 pictureBox1相同的问题。它没有足够快地加载图像。有时它确实如此。

新的表单代码:

    public partial class PictureBoxBigSize : Form
    {
        public static PictureBox pb = new PictureBox();

        public PictureBoxBigSize()
        {
            InitializeComponent();

            pictureBox1.Image = pb.Image;
            pictureBox1.Update();
        }

        private void PictureBoxBigSize_Load(object sender, EventArgs e)
        {

        }
    }

0 个答案:

没有答案