using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AsynchTest { public partial class LoadBitmapFrom : Form { public LoadBitmapFrom() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); bitmapLocationTextBox.Text = Application.StartupPath + "\\Sunset.jpg"; } private void bitmapPictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e) { cancelButton.Enabled = false; if (e.Error != null) { // An unexpected error occurred - display the details MessageBox.Show(e.Error.Message); } else if (e.Cancelled == true) { // The user cancelled the loading operation MessageBox.Show("Loading Cancelled"); } else { // The loading was completed MessageBox.Show("Loading completed"); } } private void bitmapPictureBox_LoadProgressChanged(object sender, ProgressChangedEventArgs e) { // Update the progress bar on the form loadProgressBar.Value = e.ProgressPercentage; } private void loadBitmapButton_Click_1(object sender, EventArgs e) { cancelButton.Enabled = true; bitmapPictureBox.Image = null; // This loads the bitmap asynchronously bitmapPictureBox.LoadAsync(bitmapLocationTextBox.Text); } private void cancelButton_Click_1(object sender, EventArgs e) { // Cancel the loading of the bitmap bitmapPictureBox.CancelAsync(); } } }