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

Image not shown #240

Closed
TorbenLL opened this Issue Jan 7, 2019 · 6 comments

Comments

Projects
None yet
2 participants
@TorbenLL
Copy link

TorbenLL commented Jan 7, 2019

  • .NET Core Version: 3.0 Preview1

  • Windows version: 1803

  • 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>
@vatsan-madhavan

This comment has been minimized.

Copy link
Member

vatsan-madhavan commented Jan 7, 2019

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.

@TorbenLL

This comment has been minimized.

Copy link

TorbenLL commented Jan 7, 2019

Image example.zip
I have attached code with 2 projects. One for .net 4.8 and one for .net core 3.0.

@vatsan-madhavan

This comment has been minimized.

Copy link
Member

vatsan-madhavan commented Jan 7, 2019

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.

@TorbenLL

This comment has been minimized.

Copy link

TorbenLL commented Jan 7, 2019

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

@vatsan-madhavan

This comment has been minimized.

Copy link
Member

vatsan-madhavan commented Jan 7, 2019

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.

@TorbenLL

This comment has been minimized.

Copy link

TorbenLL commented Jan 8, 2019

Thanks. It now runs.

@TorbenLL TorbenLL closed this Jan 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment