diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs index 90bbbcc9fb6..12a842263c7 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs @@ -7,7 +7,6 @@ using System.Net; using System.Runtime.InteropServices; using System.Windows.Forms.Layout; -using System.Windows.Forms.Primitives; namespace System.Windows.Forms; @@ -26,6 +25,9 @@ public partial class PictureBox : Control, ISupportInitialize !AppContext.TryGetSwitch("System.Windows.Forms.PictureBox.UseWebRequest", out bool useWebRequest) || useWebRequest; + private static readonly HttpClient s_httpClient = !LocalAppContextSwitches.ServicePointManagerCheckCrl ? new() : + new(new HttpClientHandler { CheckCertificateRevocationList = true }); + /// /// The type of border this control will have. /// @@ -464,23 +466,26 @@ public void Load() try { DisposeImageStream(); - if (UseWebRequest()) + Uri uri = CalculateUri(_imageLocation); + if (uri.IsFile) { - LoadImageViaWebClient(); + _localImageStreamReader = new StreamReader(uri.LocalPath); + Image img = Image.FromStream(_localImageStreamReader.BaseStream); + InstallNewImage(img, ImageInstallationType.FromUrl); } - else + else if (UseWebRequest()) { - Uri uri = CalculateUri(_imageLocation); - if (uri.IsFile) + // Run async operation synchronously to avoid blocking UI thread and potential deadlocks. + Task.Run(async () => { - _localImageStreamReader = new StreamReader(uri.LocalPath); - Image img = Image.FromStream(_localImageStreamReader.BaseStream); + _uriImageStream = await s_httpClient.GetStreamAsync(uri).ConfigureAwait(false); + Image img = Image.FromStream(_uriImageStream); InstallNewImage(img, ImageInstallationType.FromUrl); - } - else - { - throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported); - } + }).GetAwaiter().GetResult(); + } + else + { + throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported); } } catch