dotnet / dotnet/wpf

BitmapImage with StreamSource in XAML causes NullReferenceException

Open
#5,416 1 comment 0 reactions 1 assignee Claimed by @anjaligupta-dev View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: 5.0.303 and 6.0.100-rc.1.21463.6
* Windows version: 21H1
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
* Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No

**Problem description:**
If you use a BitmapImage with a StreamSource in xaml a null reference exception is thrown at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation. Interestingly in VS2022 P4.1 the designer actually renders the image in my use case (the resource dictionary with the BitmapImage is in a class library)

**Actual behavior:**
The offending line is at https://github.com/dotnet/wpf/blob/0dce4a07f7c51168ff0daf068b0d530a8b53085f/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapImage.cs#L308

Since _baseUri is set (it points to the xaml file that contains it, Resource dictionary, user control, window etc) it tries to use the UriSource as a relative path when it is null, setting a UriSource overrides using the stream source and defeats the purpose

**Expected behavior:**
The image to be rendered from the stream that gets passed in

**Minimal repro:**
See https://github.com/julian-altech/StreamSourceTest

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.