Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/System.Windows.Forms/src/ILLink.Substitutions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<linker>
<assembly fullname="System.Windows.Forms">
<!-- System.Windows.Forms.PictureBox.UseWebRequest enables downloading images from network. It is enabled by default. -->
<type fullname="System.Windows.Forms.PictureBox" feature="System.Windows.Forms.PictureBox.UseWebRequest" featurevalue="false">
<method signature="System.Boolean UseWebRequest()" body="stub" value="false" />
<method signature="System.Void StartLoadViaWebRequest()" body="stub" />
<method signature="System.Void LoadImageViaWebClient()" body="stub" />
</type>
</assembly>
</linker>
3 changes: 3 additions & 0 deletions src/System.Windows.Forms/src/Resources/SR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4534,6 +4534,9 @@ Stack trace where the illegal operation occurred was:
<data name="PictureBoxWaitOnLoadDescr" xml:space="preserve">
<value>Controls whether processing will stop until the image is loaded.</value>
</data>
<data name="PictureBoxRemoteLocationNotSupported" xml:space="preserve">
<value>Loading from remote location not supported.</value>
</data>
<data name="PopupControlBadParentArgument" xml:space="preserve">
<value>Cannot set the ParentPopup to be yourself.</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/System.Windows.Forms/src/Resources/xlf/SR.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/System.Windows.Forms/src/System.Windows.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<EmbeddedResource Update="Resources\System\Windows\Forms\PrintPreviewDialog.resx">
<LogicalName>System.Windows.Forms.PrintPreviewDialog.resources</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="ILLink.Substitutions.xml">
<LogicalName>ILLink.Substitutions.xml</LogicalName>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand Down
97 changes: 82 additions & 15 deletions src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// The type of border this control will have.
/// </summary>
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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))]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -597,6 +659,8 @@ private void LoadCompletedDelegate(object arg)
InstallNewImage(img, installType);
}

_fileStream?.Dispose();
_fileStream = null;
_tempDownloadStream = null;
_pictureBoxState[CancellationPendingState] = false;
_pictureBoxState[AsyncOperationInProgressState] = false;
Expand Down Expand Up @@ -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;
}
}