0

I'm learing WPF and XAML. I'm trying to show an image loaded from resources.resx.

For this, I've added an existing image to resources.resx. The identifier of the image is GoogleLogo. If I open Resources.ResX is can see that the image indeed is present in the resource file.

Now I want a window that shows this image in a Grid. The XAML is like:

<Window x:Class="WpfDemo.MainWindow"
        xmlns=...
        Title="Show Google Logo" Height="160" Width="800">

    <Grid>
        <Image Source="???"/>
    </Grid>
</Window>

How to refer to the Source in resources.ResX? What to type instead of ???

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • Try `{x:Static resources:SomeResourceClass.GoogleLogo}`with `resources` corresponding to an xmlns definition that goes to the namespace where your resx designer generated the class(es) – nalka Jan 08 '20 at 13:20
  • @nalka That won't work, because images in Resources.resx are of type `System.Drawing.Bitmap`, which you can't directly assign to the Source property of an Image element. – Clemens Jan 08 '20 at 13:22
  • Ah alright I expected the implicit converters to be able to convert it – nalka Jan 08 '20 at 13:27
  • Clemens: the source file is a .PNG. Are you saying I should not add it to the RESX? How do people usually include resources with their projects? – Harald Coppoolse Jan 08 '20 at 13:29
  • @HaraldCoppoolse Exactly. Please take a look at the answers to the original questions. The usual way to embed images files in WPF applications is by setting their Build Action to Resource, which makes them so-called assembly resources, i.e. files compiled into the output assembly. – Clemens Jan 08 '20 at 13:30
  • I just found some other answers about this. They are talking about "your resources folder". I have no resource folder in my project. However a RESX was automatically added. Is it enough to add a folder names Resources to the project and add the PNG to this folder? – Harald Coppoolse Jan 08 '20 at 13:34
  • You can put the image files into whatever project folder you want, e.g. also one named "Images". The folder name appears in the Pack URI by which you load the image resource. You may of course also use no folder at all and put the files into the top level of the project file hierarchy. – Clemens Jan 08 '20 at 13:36
  • I just found the solution: Indeed, as you suggested people should not add resources to a resx. They should add the file to the solution (preferrably in a subfolder with resources), and change its property Build Action to Resource. Then the following XAML is enough: Image source="Resources/GoogleLogo.png", where Resource is the name of the subfolder you chose. This is not one of the answers in the related question. Please remove this, so I can add this as a proper answer – Harald Coppoolse Jan 08 '20 at 13:45
  • Sorry, this has already been asked too often. I've added another original question which shows the direct XAML usage without a Binding. Be aware that `Resources/GoogleLogo.png` is still a Resource File Pack URI. It's just the XAML Parser that adds the necessary prefix automatically. – Clemens Jan 08 '20 at 13:54

0 Answers0