新建窗体,命名为"OtherForm.cs",窗体布局如下:
在窗体上放一个【PictureBox】控件,一个【Button】控件,一个进度条【ProgressBar】控件,一个【Label】控件来显示当前进度百分比
注:按钮和进度条控件之间有一个Label
按钮双击添加事件
private void button2_Click(object sender, EventArgs e)
{
try
{
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadAsync("https://img2.baidu.com/it/u=2845401203,1636781047&fm=26&fmt=auto&gp=0.jpg");
}
catch
{
MessageBox.Show("网络连接失败");
}
}
给PictureBox加上LoadProgressChanged事件,LoadAsync方法将会触发此事件,当前的进度可以从e.ProgressPercentage中取得
private void pictureBox1_LoadProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label7.Text = e.ProgressPercentage.ToString() + "%";
}