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:
- Create a folder say "img", right click on it and select add existing item add image to folder
- Go to properties of the added image, set
Build Action
asResource
andCopy To Output Directory
asCopy 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...
- Set
Build Action
toResource
- Change source to
/Resources/filename.png
- Set the size for the parent object
- 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:
- Make sure the image properties in the project has "Build Action" = "Resource", NOT "Embedded Resource"
- 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
Creating PDF Files at Runtime in C#
When Should I Use the Hashset<T> Type
How to Compare Unicode Characters That "Look Alike"
String.Isnullorempty(String) VS. String.Isnullorwhitespace(String)
Send Keys Through Sendinput in User32.Dll
C# Generic "Where Constraint" with "Any Generic Type" Definition
Does ASP.NET MVC Have Application Variables
What Do Programmers Mean When They Say, "Code Against an Interface, Not an Object."
Open File Dialog and Select a File Using Wpf Controls and C#
Post Build Event Execute Powershell
When Is Finally Run If You Throw an Exception from the Catch Block
Iis Wcf Service Hosting VS Windows Service
C# - Volatile Keyword Usage VS Lock
Differencebetween a C# Reference and a Pointer
Getting Downloads Folder in C#
Does Entity Framework 4 Code First Have Support for Identity Generators Like Nhibernate
Popup's in Selenium Webdrivers
A Timeout Occured After 30000Ms Selecting a Server Using Compositeserverselector