问题
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??
回答1:
If it truly is taking a long time to 'upload' you could use the FileSystemWatcher's changed event. Every time it is fired you increment the progressbar some fraction of the total known file size.
回答2:
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;
}
}
}
来源:https://stackoverflow.com/questions/9445669/show-a-progressbar-while-uploading-a-picture-windows-form-c