Editare

Partajați prin


PictureBox.LoadAsync Method

Definition

Loads the image asynchronously.

Overloads

LoadAsync(String)

Loads the image at the specified location, asynchronously.

LoadAsync()

Loads the image asynchronously.

LoadAsync(String)

Loads the image at the specified location, asynchronously.

public:
 void LoadAsync(System::String ^ url);
public void LoadAsync (string url);
member this.LoadAsync : string -> unit
Public Sub LoadAsync (url As String)

Parameters

url
String

The path for the image to display in the PictureBox.

Examples

The following code example demonstrates how to use the LoadAsync method. To run this example, paste the following code into a Windows Form that contains a PictureBox named pictureBox1 and a Button named startLoadButton. Make sure that the Click event for the button is associated with the startLoadButton_Click method in this example. You must change the image file path to a path that is valid on your system.

private void startButton_Click(object sender, EventArgs e)
{
    // Ensure WaitOnLoad is false.
    pictureBox1.WaitOnLoad = false;

    // Load the image asynchronously.
    pictureBox1.LoadAsync(@"http://localhost/print.gif");
}
Private Sub startLoadButton_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles startLoadButton.Click

    ' Ensure WaitOnLoad is false.
    pictureBox1.WaitOnLoad = False

    ' Load the image asynchronously.
    pictureBox1.LoadAsync("http://localhost/print.gif")

End Sub

Remarks

If the url parameter indicates a local file, the recommended format is a local file path. For example, an image file named myPicture.jpg located at c:\ would be accessed by passing c:\myPicture.jpg for the url parameter. A full path, such as http://www.contoso.com/path/images/image.jpg, or a relative path, such as ./images/image.jpg, can be used. If a relative path is used, it will be considered relative to the working directory. A call to the Load method sets the ImageLocation property to the value of the url parameter.

A call to the LoadAsync method sets the ImageLocation property to the value of url. Besides calling the LoadAsync method, you must set the WaitOnLoad property to false to load an image asynchronously. When you load an image asynchronously, you can handle the LoadProgressChanged event to determine the progress of an image load or the LoadCompleted event to determine when an image load has completed. If an error occurs during an asynchronous image-loading operation, it will be caught and reported by the Error property of the AsyncCompletedEventArgs.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Load(String).

Load behavior changes

Starting with .NET 8, the behavior of how a PictureBox control loads a remote image changed. By default, the System.Net.ServicePointManager.CheckCertificateRevocationList property is set to true before a remote image is downloaded through WebClient. This setting ensures that servers with certificates have those certificates checked against the certificate authority revocation list (CRL) as part of the validation process.

Warning

As soon as a remote image is loaded, CheckCertificateRevocationList is changed to true for the lifetime of the app. You can revert back to false manually if required, but as soon as another remote image is loaded, CheckCertificateRevocationList is set to true.

A previously working remote resource might fail to load when the locally cached CRL is out-of-date and an update can't be retrieved. This can happen when the network the app is running on is restricted and the CRL location isn't on the allowlist.

It's also possible that the delay in checking the CRL negatively affects the app's ability to function.

You can opt out of this behavior by setting the System.Windows.Forms.ServicePointManagerCheckCrl option for the app, in one of the following ways:

  • Set the property to false in the [app].runtimeconfig.json configuration file:

    {
      "configProperties": {
        "System.Windows.Forms.ServicePointManagerCheckCrl": false
      }
    }
    
  • Add a <RuntimeHostConfigurationOption> item in the project file to disable it:

    <ItemGroup>
      <RuntimeHostConfigurationOption Include="System.Windows.Forms.ServicePointManagerCheckCrl" Value="false" />
    </ItemGroup>
    

Applies to

LoadAsync()

Loads the image asynchronously.

public:
 void LoadAsync();
public void LoadAsync ();
member this.LoadAsync : unit -> unit
Public Sub LoadAsync ()

Remarks

Besides calling the LoadAsync method, the WaitOnLoad property must be set to false to load an image asynchronously. When you load an image asynchronously, you can handle the LoadProgressChanged event to determine the progress of an image load or the LoadCompleted event to determine when an image load has completed.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Load().

Load behavior changes

Starting with .NET 8, the behavior of how a PictureBox control loads a remote image changed. By default, the System.Net.ServicePointManager.CheckCertificateRevocationList property is set to true before a remote image is downloaded through WebClient. This setting ensures that servers with certificates have those certificates checked against the certificate authority revocation list (CRL) as part of the validation process.

Warning

As soon as a remote image is loaded, CheckCertificateRevocationList is changed to true for the lifetime of the app. You can revert back to false manually if required, but as soon as another remote image is loaded, CheckCertificateRevocationList is set to true.

A previously working remote resource might fail to load when the locally cached CRL is out-of-date and an update can't be retrieved. This can happen when the network the app is running on is restricted and the CRL location isn't on the allowlist.

It's also possible that the delay in checking the CRL negatively affects the app's ability to function.

You can opt out of this behavior by setting the System.Windows.Forms.ServicePointManagerCheckCrl option for the app, in one of the following ways:

  • Set the property to false in the [app].runtimeconfig.json configuration file:

    {
      "configProperties": {
        "System.Windows.Forms.ServicePointManagerCheckCrl": false
      }
    }
    
  • Add a <RuntimeHostConfigurationOption> item in the project file to disable it:

    <ItemGroup>
      <RuntimeHostConfigurationOption Include="System.Windows.Forms.ServicePointManagerCheckCrl" Value="false" />
    </ItemGroup>
    

Applies to