上传图片时显示ProgressBar(Windows Form C#)

时间:2012-02-25 16:14:14

标签: c# winforms visual-studio-2010

我有一个情况。我有一个Windows窗体的图片框,我让用户使用openfileupload控件浏览图片,之后我将所选图片设置到图片框中。这是我的代码:

namespace Employee_Card_Manager
{
public partial class Form1 : Form
{
    string Chosen_File = "";

    public Form1()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        selectpic.Title = "Browse Employee Picture!";
        selectpic.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        selectpic.FileName = "";
        selectpic.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|BITMAPS|*.bmp";

        if (selectpic.ShowDialog() != DialogResult.Cancel)
        {
            progressBar1.Enabled = true;
            Chosen_File = selectpic.FileName;
            pictureBox1.Image = Image.FromFile(Chosen_File);
            progressBar1.Enabled = false;
        }
    }
}

}

它完美运作!我需要对此代码添加一些修改,以便当用户浏览图片并按下“打开”按钮时,我的应用程序将向他显示一个进度条,该图片正在上载同时... 我找到了以下代码来显示进度条:

namespace ProgressBarSampleCSharp
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void CreateButton_Click(object sender, EventArgs e)
    {
        ProgressBar pBar = new ProgressBar();
        pBar.Location = new System.Drawing.Point(20, 20);
        pBar.Name = "progressBar1";
        pBar.Width = 200;
        pBar.Height = 30;

        //pBar.Dock = DockStyle.Bottom;

        pBar.Minimum = 0;
        pBar.Maximum = 100;
        pBar.Value = 70;

        Controls.Add(pBar);
    }
}

}

但我不知道如何将这段代码放入我的班级,以便在上传图片的同时显示进度条! 任何想法??

2 个答案:

答案 0 :(得分:0)

如果确实花了很长时间'上传',您可以使用FileSystemWatcher的更改event。每次触发时,都会将进度条增加到已知文件总大小的一小部分。

答案 1 :(得分:0)

我有一个适合回答你问题的旧代码 为了清楚起见,我将ProgressBar控件从InitializeComponent中取出 但是,我认为当您运行此代码时,您将完全删除进度条。

namespace Employee_Card_Manager 
{ 
    public partial class Form1 : Form 
    { 
        ProgressBar pBar = new ProgressBar(); 
        string Chosen_File = ""; 

        public Form1() 
        { 
            InitializeComponent(); 
            CreateProgressBar();
        } 
        private void CreateProgressBar() 
        { 
            pBar.Location = new System.Drawing.Point(20, 20); 
            pBar.Name = "progressBar1"; 
            pBar.Width = 200; 
            pBar.Height = 30; 
            pBar.BackColor = Color.Transparent;
            pBar.Minimum = 0; 
            pBar.Maximum = 100; 
            pBar.Value = 0; 
            Controls.Add(pBar); 
        } 

        private void button1_Click(object sender, EventArgs e) 
        { 
            selectpic.Title = "Browse Employee Picture!"; 
            selectpic.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            selectpic.FileName = ""; 
            selectpic.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|BITMAPS|*.bmp"; 

            if (selectpic.ShowDialog() != DialogResult.Cancel) 
            { 
                Chosen_File = selectpic.FileName; 
                pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted);
                pictureBox1.LoadProgressChanged += new ProgressChangedEventHandler(pictureBox1_LoadProgressChanged);
                pictureBox1.WaitOnLoad = false;
                pictureBox1.LoadAsynch(Chosen_file);
            } 
        } 

        private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            pBar.Value = 0;
        }

        private void pictureBox1_LoadProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pBar.Value = e.ProgressPercentage;
        }    
    }
}