2021年3月22日星期一

WPF Windows ImageBrush ImageSource Not Working

Windows app WPF/XAML/C# .NET Framework 4.5.2 running on mostly Win 10 PCs.

The following code was previously working. Now the image is no longer displayed.

<Border Name="brdPic" BorderThickness="0" CornerRadius="4">      <Border.Background>          <ImageBrush Stretch="UniformToFill" ImageSource="Resources/Images/user_pic.png" />      </Border.Background>  </Border>    string pic_path = "https://.../User123.jpg";  ImageBrush ib = new ImageBrush();  ib.Stretch = Stretch.UniformToFill;  ib.ImageSource = new BitmapImage(new Uri(pic_path, UriKind.Absolute));  brdPic.Background = ib;  

I checked the Uri is correct and I can open the picture with same Uri in browser no problem. Even in design view the picture displays correctly using either local resource or absolute Uri.

Why would this suddenly stop working?

DEBUGGING UPDATE

When I put a break on the code and check the objects:

ImageSource shows

'((System.Windows.Media.Imaging.BitmapImage)ib.ImageSource).Metadata' threw an exception of type 'System.NotSupportedException'

Border.Background shows:

ContextList = 'brdPic.Background.ContextList' threw an exception of type 'System.InvalidCastException'

CAUSE OF ISSUE

Now I remember having this same problem about a year ago, and the cause was the supported versions of TLS on the hosting server, specifically it may have disabled old versions of TLS and enforced TLS 1.2 or TLS 1.3

The following URL explains that .NET Framework versions 3.5 - 4.5.2 are hardcoded to use values SSL 3.0 and TLS 1.0. "These values cannot be changed. You must update and retarget to NET Framework 4.6 or later versions, to use TLS 1.1 and 1.2."

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls

https://stackoverflow.com/questions/66756776/wpf-windows-imagebrush-imagesource-not-working March 23, 2021 at 11:14AM

没有评论:

发表评论