Image in Wpf Button Not Visible at Runtime

Image in WPF Button not Visible at Runtime

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"

Image not displaying at runtime C# WPF

In your project:

  1. Create a folder say "img", right click on it and select add existing item add image to folder
  2. Go to properties of the added image, set Build Action as Resource and Copy To Output Directory as Copy if newer.

It worked for me.

In XAML

<Image HorizontalAlignment="Left"  Name="MyImg" Height="80" Margin="273,147,0,0" 
VerticalAlignment="Top" Width="100" Source="/img/Desert.jpg"/>

How to use image files dynamically at runtime as button content in WPF?

Simply enumerate the folder and add the file paths of interest to a List or ObservableCollection and bind it to a ListBox (or any ItemsControl of choice). Define a DataTemplate to render the image buttons. You can use the FileSystemWatcher to observe directories for changes.

MainWindow.xaml.cs

public partial class MainWindow : Window
{
private const string ImageFolderPath = @"C:\SomeImages";
public ObservableCollection<string> ImageFilePaths { get; } = new ObservableCollection<string>();

private void OnClick(object sender, RoutedEventArgs e)
{
this.ImageFilePaths.Clear();
var directoryInfo = new DirectoryInfo(ImageFolderPath);
var enumerationOptions = new EnumerationOptions();
foreach (string imageFilePath in directoryInfo.EnumerateFiles("*", enumerationOptions)
.Where(fileInfo => fileInfo.Extension is ".png" or ".jpg")
.Select(fileInfo => fileInfo.FullName))
{
this.ImageFilePaths.Add(imageFilePath);
}
}
}

MainWindow.xaml

<Window>
<Button Content="Load Images"
Click="OnClick" />
<ListBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=ImageFilePaths}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button>
<Image Height="96"
Width="96"
Source="{Binding}" />
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>

wpf button image runtime invisible

When images were added to project resources Visual Studio set Build Action to None for such files...

  1. Set Build Action to Resource
  2. Change source to /Resources/filename.png
  3. Set the size for the parent object
  4. Set Stretch for image

See working snippet

<Button x:Name="BtName" Click="Bt_Click" Height="35" Width="35">
<Image Source="/Resources/Button-Turn-Off-icon.png" Stretch="Fill" />
</Button>

WPF Button Image is not showing when I set it to the foreground

Foreground is a color of text. Since Content is an empty string, Foreground is irrelevant (there is nothing to display in that color).

Add Image as Content:

<Button x:Name="btnInkMode" Height="20" VerticalAlignment="Top" IsEnabled="True" HorizontalAlignment="Left" Width="20" Canvas.Left="554" Canvas.Top="5" Click="btnInkMode_Click" Background="#FFFFD56A">
<Button.Content>
<Image Source="../Resources/pen-icon.gif"/>
</Button.Content>
</Button>

actually you don't even need to write <Button.Content> tag. Above and below code are equivalent:

<Button x:Name="btnInkMode" Height="20" VerticalAlignment="Top" IsEnabled="True" HorizontalAlignment="Left" Width="20" Canvas.Left="554" Canvas.Top="5" Click="btnInkMode_Click" Background="#FFFFD56A">
<Image Source="../Resources/pen-icon.gif"/>
</Button>

Image shows up in Visual Studio designer but not during run-time

This was driving me crazy. I tried lots of different things based on various articles but nothing worked. My xaml window and image are in a separate DLL from the executable assembly. Not sure if that was part of my problem.

In the end I had success with:

  1. Make sure the image properties in the project has "Build Action" = "Resource", NOT "Embedded Resource"
  2. In the xaml, with the image tag selected, use the properties windows to select the Source dropdown since NOW the image appears in the dropdown list! This let visual studio format the string for me.

The string visual studio formatted for my image was:

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

where "Paperless.Common" was the name of my assembly, and "Checkbook.png" was my image that resided in a "Resources" folder.


For completeness, the above URI is a Resource File Pack URI, where the URI prefix part is automatically added by the XAML Parser.

The full URI - which you would have to use in code behind - has a pack://application: prefix and would also work in XAML:

Source="pack://application:,,,/Paperless.Common;component/Resources/Checkbook.png"


Related Topics



Leave a reply



Submit