I created an unpackaged WinUI 3 app based on this article: https://learn.microsoft.com/en-us/windows/apps/winui/winui3/create-your-first-winui3-app#unpackaged-create-a-new-project-for-an-unpackaged-c-or-c-winui-3-desktop-app.
The problem is that my window's XAML contains <Image Source="ms-appx:///Assets/Logo.png" />, and that Logo.png doesn't show in the window after publishing until I copy the image file to the Assets directory in the publish folder. But I don't want to keep images as separate files. I'd like them to be embedded in my build and to be accessed from XAML.
Whether it is realizable?
Based on Nick's link, I came up with the following solution:
class Loader: BitmapSource {
static readonly Assembly assembly = typeof(Loader).GetTypeInfo().Assembly;
public string Name {
set {
SetSource(assembly.GetManifestResourceStream(value).AsRandomAccessStream());
}
}
}
<Grid>
<Grid.Resources>
<local:Loader x:Key="Image1" Name="App1.Assets.Image.png" />
</Grid.Resources>
<Image Source="{StaticResource Image1}" />
</Grid>
The code above displays Assets\Image.png where App1 is the root namespace.
Related
I am attempting to set the image source in an effort to ultimately pull corporate standard images from what will be a DLL reference.
For testing and to ensure the proper syntax of the Uri, the image was loaded locally to a test project and the source was hard coded in the XAML.
<Image Name="imgTest" Source="pack://application:,,,/test2;component/Dictionaries/bricks.png"/>
The hardcoded value of imgTest.Source was viewed in debug mode and reads:
imgTest.Source = "pack://application:,,,/test2;component/Dictionaries/bricks.png"
Next, the source for the image was set in code.
BitmapImage imageUri = new BitmapImage();
imageUri.BeginInit();
var imageSource = new Uri(#"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Dictionaries/bricks.png", UriKind.Absolute);
imageUri.UriSource = imageSource;
imageUri.EndInit();
imgCopy.Source = imageUri;
The softcoded value of imgTest.Source was viewed in debug mode and reads:
imgTest.Source = "pack://application:,,,/test2;component/Dictionaries/bricks.png"
Both the hard coded and soft coded values are identical however the image does not render with the soft coded configuration.
Updates of the 'Build Action' property for the image have been attempted with Embedded Resource, Content, and Resource in combination with each of the three options available for the 'Copy to Output Directory' property.
Any wisdom on this issue is greatly appreciated.
Edit # 1
A side-by-side comparison where I copied the Source property to the soft coded image yields no image being displayed while the hard coded image does yet debug shows identical Source values. XAML and C# code shown.
<Image Name="imgCopy_Soft" />
<Image Name="imgCopy_Hard" Source="Dictionaries/bricks.png" />
imgCopy_Soft.Source = imgCopy_Hard.Source;
Edit # 2 Here is the full XAML
<UserControl x:Class="test2.ucConfigurator"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:test2"
mc:Ignorable="d" Height="439.5" Width="400">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="32"/>
</Grid.ColumnDefinitions>
<Image Name="imgCopy_Soft" Grid.Column="0" />
<Image Name="imgCopy_Hard" Grid.Column="1" Source="pack://application:,,,/test2;component/Dictionaries/bricks.png" />
</Grid>
</Grid>
</UserControl>
And the code behind it:
namespace test2
{
public partial class ucConfigurator : UserControl
{
public ucConfigurator()
{
InitializeComponent();
}
public void Initialize()
{
BitmapImage imageUri = new BitmapImage();
imageUri.BeginInit();
var imageSource = new Uri(#"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Dictionaries/bricks.png", UriKind.Absolute);
imageUri.UriSource = imageSource;
imageUri.EndInit();
imgCopy_Soft.Source = imageUri;
imgCopy_Soft.Source = imgCopy_Hard.Source;
}
}
}
First, the DLL needs to be added as a References in Visual Studio:
I created a library project inside the same solution and named it _ImageLibrary. I then checked the box for it so it will be added to my project like so:
Inside the DLL, I set the image to this:
I have an image called awesome-cat.jpg. It is inside of a project directory called Resources.
I can then access it like so:
<Image Source="pack://application:,,,/ImageLibrary;component/Resources/awesome-cat.jpg"/>
REMEMBER!!! The portion of the XAML above shows the ASSEMBLY NAME and NOT the project name!!!! My assembly is ImageLibrary.dll.
Now if you HAVE the DLL as a loose file, just add it to your project and then reference that in References using the Browse feature.
It should be noted, this will only work if the DLL is shipped along side your exe. If you want to embed the DLL, then that is a whole different ball game and I can show you how to do that if you ask a separate question for it.
I write the following code to show image over the button but when i run the project it is not displaying anything.
<Button x:Name="Employee">
<Button.Template>
<ControlTemplate>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image Source="images/Employee.png"
Width="50"
Height="50"/>
</Border>
</ControlTemplate>
</Button.Template>
<TextBlock>Add New Epmloyee</TextBlock>
</Button>
The relative path is correct? The folder "images" must be located in the same folder where the Window's XAML is saved.
Ensure that the Build Action for file "Employee.png" is "Resource".
I just tried your xaml code and it works fine. please make sure that you have images folder under your root directory and an image below it with name employee.png
Finally i nail the problem. The problem was that i create the folder by going directly to project folder through My Computer that's why it was not showing in Visual studio. Now I add the folder from VS and drag the images in it. And Woah! it works fine. Thanks to all of you.
I samples of FlowDocument in C# show images pointing to a location on disk in the xaml file. If I want to point source to image inside program as resource or how do i set the source setting then? I ship my program flowdocument as part of the program and I cant install images on disk. I want them as resource inside the program.
<InlineUIContainer >
<Image Margin="2,0" Width="50" Source="C:\sample.jpg" ></Image>
</InlineUIContainer>
You should set image's Build action to Resource and use following code:
<InlineUIContainer >
<Image Margin="2,0" Width="50" Source="pack://application:,,,/YourAssemblyName;Images/sample.jpg"></Image>
</InlineUIContainer>
sample.jpg must be in folder "Images" in your project in this example.
I know this is simple but I am very new in XAML.
I have two folders in my solutions
Resources\Images\font.png
Shapes\dictonary1.xaml
the soultion also has app.xaml and mainwindow.xaml
<Label ToolTip="Label">
<Label.Background>
<ImageBrush ImageSource="\Resources\Images\Font.png" />
</Label.Background>
</Label>
In my dictonary1.xaml file I add a label containing a background brush
But throws me an error
This work fine in wen I do the same in MainWindow.xaml file.
The font.png file properties
Build action is Resource and
Copy to output directory is always Copy always
I hope I am clear
Did you merge dictionary1.xaml into the mainwindow.xaml? If the image is used in dictionary1.xaml then try this:
<ImageBrush ImageSource="../Resources/Images/Font.png" />
In my c#/WPF project, I added a jpg to the resources to embed into the exe file.
Now I want to use this jpg in an image tag, something like
<xmlns:prop="clr-namespace:MyProgram.Properties"
<Image Source="{Binding Source={StaticResource prop:LogoJpg}}"
My problem: it does not work. I got no idea, how to use the image. I could use files from the hdd, but I need the image to be embedded in the exe file.
First, add the image to your project, with a Build Action of "Resource" (not EmbeddedResource). For instance, I've added an image called "logo.jpg" to a folder called Images in my main project.
Then, in XAML, you use just use that resource as follows:
<Image Source="Images\logo.jpg" />
You can also use the pack syntax for the source:
<Image Source="pack://siteoforigin:,,,/Images/logo.jpg" />
Hope this helps.