Image in WPF Button not Visible at Runtime
Asked Answered
P

12

70

I have the following XAML and my particular problem relates to the image that is to be displayed on the button

<Button  HorizontalAlignment="Right"  Grid.Row="1">
    <Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
           Height="16 " Width="16"  
      Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"
    />
</Button>

The image properties have Build Action = Resource, and Copy to output directory = Do not copy. The image shows in the designer but not at runtime.

How do I get the button image to appear at runtime?

Petaliferous answered 26/3, 2013 at 11:34 Comment(12)
Try setting the build action to Content. It may be helpfulHarkey
Tried that too. That does not help either...Petaliferous
What could possibly help is checking the output console in Visual Studio when debugging the application. Sometimes it shows helpful errors (e.g. binding errors etc.)Woermer
Usually your URI will most likely be the issue, as Daniel says look at the output window, there will most likely be complaints about the image source not found or something of that nature.Romilda
set the build-action to content and set 'copy to output directory' to 'copy always' or 'copy if newer'Elect
@Elect tried all those no joy. I have since check the 'Output' at runtime, and mscorlib.dll was throwing an IOException but giving no other information...Petaliferous
Note to editors: code formatting should be reserved for code.Hessenassau
Here is also an explaination from Microsoft about the URI scheme used for WPF in case you want to understand the underlying structure.Dandy
@Killercam: What exception was that? An InvalidCastException by any chance? I am trying to debug a similar problem, and seeing whether the symptoms match exactly with this one might help me (and other future visitors) find a solution.Randolf
Apologies @O.R.Mapper I have been on holiday. Yes, if I remember correctly this was an InvalidCastException...Petaliferous
@Killercam: I have solved my particular problem; see my other comment and the link if you're interested.Randolf
@Dandy Solved. Instead of Source="/Resources/goback.png" I was using Source=Resources/goback.png. Settings: CopyIfNewer and Content.Ligate
M
96

Change the build action to Resource. Also your pack url is wrong. Either use:

Source="pack://application:,,,/Resource/UserCost2013Open16.png"

or simply

Source="/Resource/UserCost2013Open16.png"
Mooney answered 26/3, 2013 at 12:12 Comment(5)
I have just re-added the image using the designer properties panel which I did in the first place and it provided a different URI to the one I had and was like the top one you have shown - this did not work. I re-added the image as a resource - again no luck. I then re-added the image again - this gave me a URI like the bottom one show... Taadaa! It worked, why this happened I have no idea, but it is good to know in the future. Thanks for your time.Petaliferous
@Killercam: I have now experienced something very similar; have a look at my EDIT3 in this question for a possible explanation of why re-adding may be helpful (though not one for why it worked for you only on the second attempt).Randolf
pack://application:,,,/... does not appear to be liked very much by .XAML at runtime. Throws an error - "unrecognized/invalid blah blah". I have a UIDomain project with all the views, presenters, models and images. It seems that the Resources/*.png is expected to be in the views/ ... ... /Resources/img.png directory when it is more like ../Resources/img.pngPenutian
Worked for me :)Zita
Just Right Click on Image and select Properties and change "Build Action" to "Resource".Adali
I
37

There are 2 Solutions:

1: Change the settings of the image:

Build Action = Content
Copy to output directory = Copy if newer
Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"


2: When Using application instead of siteoforigin in the source path, you have to possible ways:

a) Image will be in a SubFolder called "Resources" and .exe file will be small

Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Content
Copy to output directory = Copy if newer

b) Image will be included in the .exe and no Subfolder with imagefile will exist

Source="pack://application:,,,/Resources/UserCost2013Open16.png"
Build Action = Resource
Copy to output directory = Copy if newer
Inbeing answered 26/3, 2013 at 12:18 Comment(2)
Solution 2 works. Copy to output directory is not necessary in my case.Brine
Worked. Thanks.Charie
T
14

Assuming that you have set your Build Action to Resource.

Use the URI-PACK-FORMAT:

pack://application:,,,/ResourceFile.xaml or pack://application:,,,/Subfolder/ResourceFile.xaml or pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

Those are the most common examples.

Also, in my case it was still not showing.

Clean & Rebuild NOT just Build fixed it for me (VS 2019)!

Tatia answered 13/6, 2016 at 11:13 Comment(5)
So you have the exact same setup like in the initial question and you didn't need to change the build action nor copy settings or the source path?Vanadium
Yes. BuildAction & UriPack was right. Image did not show => Clean & Rebuild and it worked.Tatia
The problem is Image in WPF Button not Visible at Runtime that was the case at my side and above is my solution. I don't care about -2 rep but I really got no idea what ur problem with this is .. ?!Tatia
set your Build Action to Resource was enought for meInapposite
This worked for me, using VS 22 and .net 6Fayalite
C
8

In my case I had the images in a separate project named Common and the images were under a folder named Resources in this project. In my other project, I added a reference to Common and set the source of the images like this:

<Image Source="/Common;component/Resources/anImage.png"/>

The images have the Build Action set to Resource and Copy to Output Directory to Do not copy. However, for some strange reason it wasn't working until I deleted every assembly file in my solution and made a Clean Solution and Build Solution. Not sure why, but it all started working at runtime once I rebuilt everything. I still can't figure out why it was working at Design Time though.

Coralloid answered 23/4, 2014 at 4:36 Comment(2)
Adding the component string into the Source path was critical to getting my images to display at run time.Surgical
Although having Content and Always Copy used to work for me, after moving to a different machine, I only saw the images/fonts in the designer and not at run-time. Changing them to Resource and Do not Copy fixed it.Belief
O
7

In Visual Studio do these steps

  1. Add your image to a folder named as you want, in this example I created an Assets named folder.
  2. Then set the image's property to Resource in the Build Action.
  3. Then select the image in the folder and drag and then drop the into <MenuItem.Icon> and it should path properly.

Drop Image into project, Set as Resource then drop onto page

  • Note you may need to Rebuild the project, the compiler sometimes doesn't recognize new resources as a need to rebuild.
Ooze answered 5/11, 2021 at 22:9 Comment(0)
S
6

Go to your image in the resources folder, right click on the image, go to properties, click on the Build Action property, and change it from None to Resource. That'll work.

Stutz answered 9/9, 2015 at 10:7 Comment(1)
Thank you so much!! Why doesn't MS's official documents say this!??!?!? :)Saransarangi
D
4

You should add any thing inside Solution Explorer of Visual Studio. Instead of just copying the image to folder in Windows Explorer, press Right Click on any folder in Solution Explorer go to Add > Existing Item... and select the path to your resource to be added.

Danadanae answered 1/8, 2015 at 8:27 Comment(0)
G
4

I defined my image as next:

<Image Source="/Resources/Images/icon.png"/>

The image is displayed in Visual Studio designer but no image is displayed when I launched the app! It made me nuts! I tried all Build Actions with clean/build, no luck.

In my case the problem is caused by the fact that the control (which uses Image) and the app itself are in different projects. Resources/Images folder is in the Controls project. As result the app attempted to find icon.png in its own Debug folder, but actually it is in Controls' Debug folder.

So two solutions work for me:

1) put Resources/Images in the app's project (not so good when there are several projects which use controls from Controls project, but it works)

2) specify the name of Controls project explicitly inside Image:

<Image Source="/Controls;component/Resources/Images/icon.png"/>
Glasswork answered 13/1, 2017 at 11:0 Comment(0)
S
0

Make a new folder and put your pictures in the new folder and write this in XAML

<Image Source="/newfolder/icon.png"/>
Stralka answered 15/4, 2020 at 13:55 Comment(0)
M
0

For me, changing the "build action" to "Resource" and the "copy to output directory" to "Do not copy" has solved my problem.

Monika answered 3/11, 2022 at 10:47 Comment(0)
C
0

In case anyone arrives here and has the image set as "Resource" and "Do not copy" and it still doesn't work, I was able to fix this by toggling Build Action for the image off of "Resource" and then back again.

Cultivable answered 9/6, 2023 at 19:54 Comment(0)
S
-6

Source="file:///D:/100x100.jpg"/> works for me.

Slightly answered 27/5, 2018 at 5:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.