Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to not use WebRequest for PictureBox #6684

Merged
merged 10 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
121 changes: 106 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 Down Expand Up @@ -466,26 +468,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)
{
_localImageStreamReader = new StreamReader(uri.LocalPath);
Image img = Image.FromStream(_localImageStreamReader.BaseStream);
InstallNewImage(img, ImageInstallationType.FromUrl);
}
else
{
_uriImageStream = wc.OpenRead(uri.ToString());
img = Image.FromStream(_uriImageStream);
throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported);
}
}
}
Expand All @@ -498,12 +499,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 +570,74 @@ 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()
{
Task.Run(() =>
{
try
{
using var stream = File.OpenRead(_imageLocation);
int bytesRead = -1;
do
{
var memory = new Memory<byte>(_readBuffer);
bytesRead = await stream.ReadAsync(memory, CancellationToken.None);
if (bytesRead > 0)
{
_totalBytesRead += bytesRead;
_tempDownloadStream.Write(_readBuffer, 0, bytesRead);

// Report progress thus far, but only if we know total length.
if (_contentLength != -1)
{
int progress = (int)(100 * (((float)_totalBytesRead) / ((float)_contentLength)));
if (_currentAsyncLoadOperation is not null)
{
_currentAsyncLoadOperation.Post(_loadProgressDelegate, new ProgressChangedEventArgs(progress, null));
}
}
}
else
{
_tempDownloadStream.Seek(0, SeekOrigin.Begin);
if (_currentAsyncLoadOperation is not null)
{
_currentAsyncLoadOperation.Post(_loadProgressDelegate, new ProgressChangedEventArgs(100, null));
}
}
}
while (bytesRead > 0);
PostCompleted(null, false);
}
catch (Exception error)
{
// Since this is on a non-UI thread, we catch any exceptions and
// pass them back as data to the UI-thread.
PostCompleted(error, 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 @@ -1193,5 +1281,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;
}
}