上传图片时显示进度条(Windows Form C#) [英] Show a ProgressBar while uploading a picture (Windows Form C#)

查看:35
本文介绍了上传图片时显示进度条(Windows Form C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个情况.我有一个 windows 形式的图片框,我让用户使用 openfileupload 控件浏览图片,然后我将选定的图片设置到图片框中.这是我的代码:

I have a situation here. I have a picture box in windows form and i let user to browse a picture by using openfileupload control and after that i set selected picture into picture box. Here is my code:

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

}

它工作得很好!我需要对此代码进行一些修改,以便当用户浏览图片并按下打开"按钮时,我的应用程序会向他显示一个进度条,表明该图片正在同时上传...我找到了以下代码来显示进度条:

It is working perfectly! I need to add some modification to this code so that when user browse a picture and presses Open button my application will show him a progress bar that this picture is being uploaded in mean time... I have found the following code to show a progress bar:

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

}

但我不知道如何将此代码放入我的课程中,以便在上传图片的同时显示进度条!有什么想法吗??

But i have no idea how to fit this code into my class so that it will show progress bar in the mean time when picture is being uploaded! any ideas??

推荐答案

我有一个旧代码可以回答你的问题.
为了清楚起见,我让 ProgressBar 控件脱离了 InitializeComponent.
但是,我认为当您运行此代码时,您将完全删除进度条.

I have an old code adapted to answer your question.
I let the ProgressBar control out of the InitializeComponent just for clarity.
However, I think that when you run this code, you'll remove the progress bar completely.

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

这篇关于上传图片时显示进度条(Windows Form C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆