winforms 上载图片时显示ProgressBar(Windows窗体C#)

8yoxcaq7  于 2022-11-16  发布在  Windows
关注(0)|答案(2)|浏览(150)

我这里有一个情况。我有一个图片框在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);
    }
}

}
但是我不知道如何把这个代码放进我的类中,这样它就会在图片上传的同时显示进度条!有什么想法吗?

piv4azn7

piv4azn71#

我有一个旧的密码来回答你的问题。
为了清楚起见,我将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;
        }    
    }
}
tuwxkamq

tuwxkamq2#

如果确实需要很长的时间来“上传”,你可以使用FileSystemWatcher的修改event。每次它被激发时,你增加进度条的一部分总的已知文件大小。

相关问题