diff --git a/src/System.Windows.Forms/src/ILLink.Substitutions.xml b/src/System.Windows.Forms/src/ILLink.Substitutions.xml
new file mode 100644
index 00000000000..042eadd6c37
--- /dev/null
+++ b/src/System.Windows.Forms/src/ILLink.Substitutions.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/src/System.Windows.Forms/src/Resources/SR.resx b/src/System.Windows.Forms/src/Resources/SR.resx
index 5971d6d8975..2c173d935dd 100644
--- a/src/System.Windows.Forms/src/Resources/SR.resx
+++ b/src/System.Windows.Forms/src/Resources/SR.resx
@@ -4534,6 +4534,9 @@ Stack trace where the illegal operation occurred was:
Controls whether processing will stop until the image is loaded.
+
+ Loading from remote location not supported.
+
Cannot set the ParentPopup to be yourself.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf
index bab0b653647..c86cb7fba26 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf
@@ -7696,6 +7696,11 @@ Trasování zásobníku, kde došlo k neplatné operaci:
Událost aktivovaná při změně hodnoty vlastnosti SizeMode ovládacího prvku PictureBox
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Určuje, jak prvek PictureBox ošetří umístění obrázku a nastavení velikosti ovládacího prvku.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf
index 5a5fcd5ec86..afc89378a09 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf
@@ -7696,6 +7696,11 @@ Stapelüberwachung, in der der unzulässige Vorgang auftrat:
Das ausgelöste Ereignis, wenn der Wert der SizeMode-Eigenschaft für PictureBox geändert wird.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Steuert, wie die PictureBox die Bildplatzierung und die Größenanpassung für das Steuerelement behandelt.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf
index 901cf844030..22870dde77b 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf
@@ -7696,6 +7696,11 @@ El seguimiento de la pila donde tuvo lugar la operación no válida fue:
Evento que se desencadena cuando se cambia el valor de la propiedad SizeMode de PictureBox.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Controla cómo tratará PictureBox la ubicación de las imágenes y el tamaño del control.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf
index 4f7f650645e..e357aec4008 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf
@@ -7696,6 +7696,11 @@ Cette opération non conforme s'est produite sur la trace de la pile :
Événement déclenché lorsque la valeur de la propriété SizeMode du PictureBox est modifiée.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Contrôle la façon dont le PictureBox gère le placement de l'image et le dimensionnement du contrôle.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf
index 05733952962..62f32da694e 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf
@@ -7696,6 +7696,11 @@ Traccia dello stack da cui si è verificata l'operazione non valida:
Evento generato quando il valore della proprietà SizeMode di PictureBox viene modificato.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Determina in che modo il controllo PictureBox gestirà la posizione e il dimensionamento dell'immagine.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf
index 0045a1c5cc6..e532957a9a3 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf
@@ -7696,6 +7696,11 @@ Stack trace where the illegal operation occurred was:
SizeMode プロパティの値が PictureBox で変更されたときに発生するイベントです。
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.PictureBox がイメージの配置とコントロール サイズの変更をどのように扱うかを制御します。
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf
index 12857c4b8ca..e5964c93010 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf
@@ -7696,6 +7696,11 @@ Stack trace where the illegal operation occurred was:
SizeMode 속성 값이 PictureBox에서 변경되면 이벤트가 발생합니다.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.PictureBox에서 이미지 배치와 컨트롤 크기 조정을 처리하는 방법을 제어합니다.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf
index 72075b77271..181fe161e58 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf
@@ -7696,6 +7696,11 @@ Stos śledzenia, w którym wystąpiła zabroniona operacja:
Zdarzenie wywoływane, gdy wartość właściwości SizeMode zostanie zmieniona w elemencie PictureBox.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Określa, w jaki sposób element PictureBox będzie obsługiwać umieszczanie obrazu i zmianę rozmiaru formantu.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf
index 3167ed154d8..6e62dc12f03 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf
@@ -7696,6 +7696,11 @@ Rastreamento de pilha em que a operação ilegal ocorreu:
Evento gerado quando o valor da propriedade SizeMode é alterado em PictureBox.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Controla como PictureBox tratará o posicionamento de imagem e o dimensionamento de controle.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf
index 800393bf738..ac2c17a0549 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf
@@ -7697,6 +7697,11 @@ Stack trace where the illegal operation occurred was:
Событие, возникающее при изменении свойства SizeMode в элементе PictureBox.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.Определяет, как будет обрабатываться размещение рисунка в данной области рисунка и изменение размеров элемента управления.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf
index a6c25df380e..18a55b1a5a6 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf
@@ -7696,6 +7696,11 @@ Geçersiz işlemin gerçekleştiği yığın izi:
PictureBox'da SizeMode özelliğinin değeri değiştiğinde harekete geçirilen olay.
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.PictureBox'ın resim yerleşimini ve denetim boyutlandırmayı nasıl işleyeceğini denetler.
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf
index 549f9f35bf5..d6c2ede52dc 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf
@@ -7696,6 +7696,11 @@ Stack trace where the illegal operation occurred was:
在 PictureBox 的 SizeMode 属性值更改时引发的事件。
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.控制 PictureBox 将如何处理图像位置和控件大小。
diff --git a/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf b/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf
index 257f361b0c2..fe98ff6dbe5 100644
--- a/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf
+++ b/src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf
@@ -7696,6 +7696,11 @@ Stack trace where the illegal operation occurred was:
SizeMode 屬性值在 PictureBox 上變更時引發的事件。
+
+ Loading from remote location not supported.
+ Loading from remote location not supported.
+
+ Controls how the PictureBox will handle image placement and control sizing.控制 PictureBox 如何處理影像放置位置以及控制大小。
diff --git a/src/System.Windows.Forms/src/System.Windows.Forms.csproj b/src/System.Windows.Forms/src/System.Windows.Forms.csproj
index c9b98584861..2323dba7a43 100644
--- a/src/System.Windows.Forms/src/System.Windows.Forms.csproj
+++ b/src/System.Windows.Forms/src/System.Windows.Forms.csproj
@@ -52,6 +52,9 @@
System.Windows.Forms.PrintPreviewDialog.resources
+
+ ILLink.Substitutions.xml
+
diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs
index c632d50c345..e65c1a96ea5 100644
--- a/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs
+++ b/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs
@@ -26,6 +26,8 @@ namespace System.Windows.Forms
[SRDescription(nameof(SR.DescriptionPictureBox))]
public partial class PictureBox : Control, ISupportInitialize
{
+ private static readonly bool s_useWebRequest = AppContext.TryGetSwitch("System.Windows.Forms.PictureBox.UseWebRequest", out bool useWebRequest) ? useWebRequest : true;
+
///
/// The type of border this control will have.
///
@@ -48,6 +50,7 @@ public partial class PictureBox : Control, ISupportInitialize
// Instance members for asynchronous behavior
private AsyncOperation _currentAsyncLoadOperation;
+ private FileStream _fileStream;
private string _imageLocation;
private Image _initialImage;
private Image errorImage;
@@ -466,26 +469,25 @@ public void Load()
// false to prevent subsequent attempts.
_pictureBoxState[NeedToLoadImageLocationState] = false;
- Image img;
- ImageInstallationType installType = ImageInstallationType.FromUrl;
try
{
DisposeImageStream();
-
- Uri uri = CalculateUri(_imageLocation);
- if (uri.IsFile)
+ if (UseWebRequest())
{
- _localImageStreamReader = new StreamReader(uri.LocalPath);
- img = Image.FromStream(_localImageStreamReader.BaseStream);
+ LoadImageViaWebClient();
}
else
{
-#pragma warning disable SYSLIB0014 // Type or member is obsolete
- using (WebClient wc = new WebClient())
-#pragma warning restore SYSLIB0014 // Type or member is obsolete
+ Uri uri = CalculateUri(_imageLocation);
+ if (uri.IsFile)
{
- _uriImageStream = wc.OpenRead(uri.ToString());
- img = Image.FromStream(_uriImageStream);
+ _localImageStreamReader = new StreamReader(uri.LocalPath);
+ Image img = Image.FromStream(_localImageStreamReader.BaseStream);
+ InstallNewImage(img, ImageInstallationType.FromUrl);
+ }
+ else
+ {
+ throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported);
}
}
}
@@ -498,12 +500,32 @@ public void Load()
else
{
// In design mode, just replace with Error bitmap.
- img = ErrorImage;
- installType = ImageInstallationType.ErrorOrInitial;
+ InstallNewImage(ErrorImage, ImageInstallationType.ErrorOrInitial);
+ }
+ }
+ }
+
+ private void LoadImageViaWebClient()
+ {
+ Image img;
+ Uri uri = CalculateUri(_imageLocation);
+ if (uri.IsFile)
+ {
+ _localImageStreamReader = new StreamReader(uri.LocalPath);
+ img = Image.FromStream(_localImageStreamReader.BaseStream);
+ }
+ else
+ {
+#pragma warning disable SYSLIB0014 // Type or member is obsolete
+ using (WebClient wc = new WebClient())
+#pragma warning restore SYSLIB0014 // Type or member is obsolete
+ {
+ _uriImageStream = wc.OpenRead(uri.ToString());
+ img = Image.FromStream(_uriImageStream);
}
}
- InstallNewImage(img, installType);
+ InstallNewImage(img, ImageInstallationType.FromUrl);
}
[SRCategory(nameof(SR.CatAsynchronous))]
@@ -549,7 +571,47 @@ public void LoadAsync()
_pictureBoxState[CancellationPendingState] = false;
_contentLength = -1;
_tempDownloadStream = new MemoryStream();
+ if (UseWebRequest())
+ {
+ StartLoadViaWebRequest();
+ }
+ else
+ {
+ var uri = CalculateUri(_imageLocation);
+ if (uri.IsFile)
+ {
+ LoadFromFileAsync();
+ }
+ else
+ {
+ throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported);
+ }
+ }
+ }
+
+ private void LoadFromFileAsync()
+ {
+ try
+ {
+ _fileStream = File.OpenRead(_imageLocation);
+ _contentLength = (int)_fileStream.Length;
+ _totalBytesRead = 0;
+ _fileStream.BeginRead(
+ _readBuffer,
+ 0,
+ ReadBlockSize,
+ new AsyncCallback(ReadCallBack),
+ _fileStream);
+ }
+ catch (Exception error)
+ {
+ PostCompleted(error, cancelled: false);
+ }
+ }
+
+ private void StartLoadViaWebRequest()
+ {
#pragma warning disable SYSLIB0014 // Type or member is obsolete
WebRequest req = WebRequest.Create(CalculateUri(_imageLocation));
#pragma warning restore SYSLIB0014 // Type or member is obsolete
@@ -597,6 +659,8 @@ private void LoadCompletedDelegate(object arg)
InstallNewImage(img, installType);
}
+ _fileStream?.Dispose();
+ _fileStream = null;
_tempDownloadStream = null;
_pictureBoxState[CancellationPendingState] = false;
_pictureBoxState[AsyncOperationInProgressState] = false;
@@ -1193,5 +1257,8 @@ void ISupportInitialize.EndInit()
_pictureBoxState[InInitializationState] = false;
}
+
+ // The Linker is also capable of replacing the value of this method when the application is being trimmed.
+ private static bool UseWebRequest() => s_useWebRequest;
}
}