Does the bug reproduce also in WPF for .NET Framework 4.8?: No
Problem description:
WPF in .net core does not show an image
Actual behavior:
The code compile and run but the image given is not shown.
It may be due to the image file is no longer compiled with the "resource" switch
Expected behavior:
The image is shown in the window at run time.
Minimal repro:
<Window x:Class="MyWPFApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWPFApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image Stretch="Fill" Width="50" Height="50" >
<Image.Source>
<BitmapImage UriSource="Example.bmp"/>
</Image.Source>
</Image>
</Grid>
</Window>
What does your project look like, and where is Example.bmp in your project relative to other source files?
The _Minimal repro_ as presented requires additional information before we can actually reproduce the problem. Perhaps a link to a GitHub repo that contains a _buildable_ repro project would be more helpful in this instance.
Image example.zip
I have attached code with 2 projects. One for .net 4.8 and one for .net core 3.0.
The problem seems to be in your .NET Core project, you are including Example.bmp as EmbeddedResource whereas in the .NET 4.8 project, you are including it as a Resource. Just include the BMP as a Resource in the .NET Core project and it will work the same - like this:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<None Remove="Example.bmp" />
</ItemGroup>
<ItemGroup>
<!-- Don't do this -->
<!-- <EmbeddedResource Include="Example.bmp"> -->
<!-- <CopyToOutputDirectory>Never</CopyToOutputDirectory> -->
<!-- </EmbeddedResource> -->
<!-- Instead of "EmbeddedResource", Use "Resource" -->
<Resource Include="Example.bmp" />
</ItemGroup>
</Project>
If you chose EmbeddedResource because that is the only option available in the VS IDE, please note that full IDE support for WPF (and WinForms) on .NET Core is in the works. For now, we expect users of the preview SDK to hand-craft the project and not fully rely upon on the IDE to get things right.
Thanks with your changes I have it running.
However what I am trying to do is accessing pictures that are in a separate assembly.
In the attached example I have added a new project that simple holds a picture and then I want to use that in the exe program. Is there a way to do that?
Image example v2.zip
Change the the ResourceAssemb project type to Sdk="Microsoft.NET.Sdk.WindowsDesktop". You have it currently set up as a Sdk="Microsoft.NET.Sdk". That should take care of embedding the bitmap into the assembly correctly.
Your project in using Resource in a manner that is specific to WPF, so it would need appropriate WPF related targets made available by the WindowsDesktop SDK to build it.
Thanks. It now runs.
There is still no Resource as a drop down in the IDE. Is this now an issue with the IDE not supporting Resource?
There is still no
Resourceas a drop down in the IDE. Is this now an issue with the IDE not supportingResource?
@chabiss?