private void DownloadAsync(string url)
{
if (thread != null)
return;
thread = new Thread(() =>
{
okButton.Invoke((Action)(() =>
{
okButton.Text = "Downloading...";
okButton.Enabled = false;
}));
try
{
using (WebClient webClient = new WebClient())
downloadData = webClient.DownloadData(url);
memoryStream = new MemoryStream(downloadData);
ResultImage = (Bitmap)Bitmap.FromStream(memoryStream);
DialogResult = DialogResult.OK;
}
catch (WebException)
{
errorLabel.Invoke((Action)(() =>
{
errorLabel.Text = "Failed to download image. Check the URL and try again.";
errorLabel.Visible = true;
}));
}
catch (ArgumentException)
{
errorLabel.Invoke((Action)(() =>
{
errorLabel.Text = "The downloaded file is not a valid image. Check that the URL is a direct image link.";
errorLabel.Visible = true;
}));
}
catch (ThreadAbortException)
{
}
finally
{
thread = null;
try
{
okButton.Invoke((Action)(() =>
{
okButton.Text = "OK";
okButton.Enabled = true;
}));
}
catch (Exception)
{
}
}
});
thread.IsBackground = true;
thread.Start();
}