Image in WPF button not showing in runtime mode

That's all, I have the following start to a small application that checks .resx files for consistency of the built-in brackets (so that runtime errors of inappropriate lines of "... {0}" do not occur). I have the following XAML for MainWindow.xaml and my specific problem is with the image that should be displayed on the button

<Window x:Class="ResxChecker.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="174.383" Width="495.869"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="350*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="30*"/> </Grid.RowDefinitions> <Label Content="Select .resx file:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="24" Width="Auto" Grid.ColumnSpan="1"/> <TextBox Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/> <Button Grid.Column="2" HorizontalAlignment="Right" Margin="5,0,10,0" Grid.Row="1"> <Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="16 " Width="16" Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"/> </Button> </Grid> </Window> 

The image has "Build Action = Resource", "Copy to output directory = Do not copy" - the image is displayed in the designer, but not at runtime. I saw the following questions and read the corresponding answers, but no one solves the problem:

How to get button image at runtime?

+44
c # image button wpf
Mar 26 '13 at 11:34
source share
7 answers

Change the assembly action to Resource. Also the wrong url of your package. Or use:

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

or simply

 Source="/Resource/UserCost2013Open16.png" 
+53
Mar 26 '13 at 12:12
source share
β€” -

There are 2 solutions:

1: Change the image settings:

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


2: When using the application instead of siteoforigin in the source path, you need to:

a) The image will be in SubFolder under the name "Resources", and the .exe file will be small.

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

b) The image will be included in the .exe, and no subfolder with the image file will exist

 Source="pack://application:,,,/Resources/UserCost2013Open16.png" Build Action = Resource Copy to output directory = Copy if newer 
+25
Mar 26 '13 at 12:18
source share

In my case, I had 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 link to Common and set the image source as follows:

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

Build Action images are set to Resource and Copy to Output Directory to Do not copy . However, for some strange reason, it did not work until I deleted every build file in my solution and made Clean Solution and Build Solution . Not sure why, but it all started at runtime as soon as I rebuilt everything. I still can't understand why he worked at Design Time, though.

+6
Apr 23 '14 at 4:36 on
source share

Suppose you have

In my case, it was still not showing.

Clean & Rebuild Not just Build fixed it for me!

+6
Jun 13 '16 at 11:13
source share

Go to your image in the resources folder, right-click on the image, go to properties. Click the BuildAction property and set it to Resource of none. This will work.

+2
Sep 09 '15 at 10:07
source share

You must add any thing inside Visual Studio Solution Browser . Instead of just copying the image to a folder in Windows Explorer, right-click on any folder in Solution Explorer, go to Add> Existing Item ... and select the path to the resource to be added.

+1
Aug 01 '15 at 8:27
source share

I defined my image as follows:

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

The image is displayed in the designer of Visual Studio, but when the application starts, the image is not displayed! It made me crazy! I tried all Build Actions using clean / build, no luck.

In my case, the problem is that the control (which uses Image) and the application itself are in different projects. Resources / Pictures are in the Controls project. As a result, the application tried to find icon.png in its own Debug folder, but actually it is in the Debug folder from Controls.

So, two solutions work for me:

1) put resources / images in the application project (not so good when there are several projects that use the controls from the Controls project, but it works)

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

 <Image Source="/Controls;component/Resources/Images/icon.png"/> 
+1
Jan 13 '17 at 11:00
source share



All Articles