Having issues with getting images to render when using EXE (WPF)

Hello,

id like to start off by saying I’m getting myself familiar with PowerShell Pro Tools and WPF so this issue may be blatantly obvious.

in Visual Studio 2019, the image loads as I would expect. but when I compile to EXE the image is missing.

I was able to get it to load but only when I added the full path.


hoping someone can point me in the right direction.

I have a work around though a bit redundant to name all the objects in order to load an image.
I especially find this troublesome when dealing with font families.

Maybe there is a better way or i’m simply over looking a key part?

<Window

        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" 
 

  Title="MainWindow" Height="500" Width="900"
    WindowStyle="None"
    ResizeMode="NoResize"
    Background="#181735">
 
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Grid x:Name="TitleBar"
                Background="#0F0F2D"
                Grid.ColumnSpan="2" 
                MouseDown="TitleBar_MouseDown">

            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <TextBlock Text="BunnyCo"
                       Foreground="White"
                       Grid.Column="1"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       FontFamily="Fonts/#Dosis"
                       FontWeight="Medium"/>
            <TextBlock Text="Menu"
                       Foreground="LightGray"
                       Grid.Column="2"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       FontFamily="Fonts/#Dosis"/>
            <TextBlock Text="WebSite"
                       Foreground="LightGray"
                       Grid.Column="3"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       FontFamily="Fonts/#Dosis"/>

            <Button Content="Close"
                        x:Name="CloseButton"
                        Foreground="White"
                        FontSize="10"
                        FontWeight="Medium"
                        Grid.Column="12"
                        Grid.ColumnSpan="2" Click="CloseButton_Click">
                <Button.Style>
                    <Style TargetType="Button">
                        <Setter Property="TextElement.FontFamily" Value="Fonts/#Dosis"/>
                        <Setter Property="Background" Value="#FE6584"/>
                        <Setter Property="Cursor" Value="Hand"/>

                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#FF4C70"/>
                            </Trigger>
                        </Style.Triggers>

                    </Style>
                </Button.Style>
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Border Width="75" Height="15"
                                    CornerRadius="8"
                                    Background ="{TemplateBinding Background}">

                            <ContentPresenter VerticalAlignment="Center"
                                                  HorizontalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>

        </Grid>

        <StackPanel Grid.Row="1"
                    VerticalAlignment="Center"
                    Margin="60">

            <StackPanel Orientation="Horizontal"
                        Margin="0,0,0,20">
                
                <Image x:Name="LogoImage"  
                       Height="50" 
                       Width="50" 
                       Source="/Images/Box-03.png"/>
                

                <TextBlock Text="BunnyCo"
                           Foreground="White"
                           FontSize="20"
                           FontWeight="Medium"
                           VerticalAlignment="Center"
                           FontFamily="Fonts/#Dosis"
                           Margin="10,0,0,0"/>
            </StackPanel>

            <StackPanel>
                <TextBlock Text="Customize"
                       Foreground="White"
                       FontSize="25"
                       FontWeight="Medium"
                       FontFamily="Fonts/#Dosis"/>

                <TextBlock Text="Your"
                       Foreground="White"
                       FontSize="25"
                       FontWeight="Medium"
                       FontFamily="Fonts/#Dosis"/>

                <TextBlock Text="Experience"
                       Foreground="White"
                       FontSize="25"
                       FontWeight="Medium"
                       FontFamily="Fonts/#Dosis"/>

                <TextBlock Text="Lorem ipsum dolor sit amet. Est earum deserunt non beatae cupiditate et nesciunt voluptatem sed nulla repudiandae vel suscipit voluptatibus. Qui molestiae architecto rem obcaecati Quis et ipsam amet id quas saepe cum repudiandae internos non porro quibusdam et galisum unde. Id galisum praesentium quo numquam vitae et excepturi aperiam consequatur illo. Qui doloremque excepturi ut mollitia mollitia At quisquam voluptas eum dolorem numquam qui dolor asperiores est sapiente enim."
                       Foreground="White"
                       FontSize="12"
                       FontWeight="Medium"
                       FontFamily="Fonts/#Dosis"
                       TextWrapping="Wrap"
                           Margin="0,10,0,0"/>
            </StackPanel>

            <StackPanel Orientation="Horizontal"
                        Margin="0,15,0,0">
                <Button Content="Start"
                        Foreground="White"
                        FontSize="14"
                        FontWeight="Medium"
                        Margin="0,0,10,0">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="TextElement.FontFamily" Value="Fonts/#Dosis"/>
                            <Setter Property="Background" Value="#FE6584"/>
                            <Setter Property="Cursor" Value="Hand"/>

                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#FF4C70"/>
                                </Trigger>
                            </Style.Triggers>

                        </Style>
                    </Button.Style>
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Border Width="100" Height="25"
                                    CornerRadius="12"
                                    Background ="{TemplateBinding Background}">

                                <ContentPresenter VerticalAlignment="Center"
                                                  HorizontalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <Button Content="Register"
                        Foreground="White"
                        FontSize="14"
                        FontWeight="Medium">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="TextElement.FontFamily" Value="Fonts/#Dosis"/>
                            <Setter Property="Background" Value="#39385D"/>
                            <Setter Property="Cursor" Value="Hand"/>

                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#3A3952"/>
                                </Trigger>
                            </Style.Triggers>

                        </Style>
                    </Button.Style>
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Border Width="100" Height="25"
                                    CornerRadius="12"
                                    Background ="{TemplateBinding Background}">

                                <ContentPresenter VerticalAlignment="Center"
                                                  HorizontalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </StackPanel>

        </StackPanel>

        <Grid Grid.Column="1"
              Grid.RowSpan="2"
              Margin="160,-365,-610,85"
              Panel.ZIndex="-1">

            <Grid.RenderTransform>
                <RotateTransform Angle="50"/>
            </Grid.RenderTransform>

            <StackPanel Orientation="Horizontal"
                        Margin="-350,250,-255,-250">
                <StackPanel>
                    <Border Width="700"
                                            Height="35"
                                            Background="#3DDDC2"
                                            Margin="150,0,0,0"
                                            CornerRadius="20"/>

                    <Border Width="700"
                                            Height="35"
                                            Background="#FC6586"
                                            Margin="0,15,0,0"
                                            CornerRadius="20"/>

                    <Border Width="700"
                                            Height="35"
                                            Background="#383660"
                                            Margin="75,15,0,0"
                                            CornerRadius="20"/>
                </StackPanel>

                <StackPanel>
                    <Border Width="700"
                                            Height="35"
                                            Background="#3DDDC2"
                                            Margin="150,0,0,0"
                                            CornerRadius="20"/>

                    <Border Width="700"
                                            Height="35"
                                            Background="#FC6586"
                                            Margin="0,15,0,0"
                                            CornerRadius="20"/>

                    <Border Width="700"
                                            Height="35"
                                            Background="#383660"
                                            Margin="75,15,0,0"
                                            CornerRadius="20"/>
                </StackPanel>
            </StackPanel>

        </Grid>

    </Grid>

</Window>
function Add-ControlVariables {
New-Variable -Name 'LogoImage' -Value $window.FindName('LogoImage') -Scope 1 -Force
New-Variable -Name 'CloseButton' -Value $window.FindName('CloseButton') -Scope 1 -Force	
New-Variable -Name 'TitleBar' -Value $window.FindName('TitleBar') -Scope 1 -Force
}

[System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework") | Out-Null

function Import-Xaml {
    [xml]$xaml = Get-Content -Path $PSScriptRoot\WPFDemo1.xaml
    $manager = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xaml.NameTable
    $manager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
    $xaml.SelectNodes("//*[@x:Name='CloseButton']", $manager)[0].RemoveAttribute('Click')
    $xaml.SelectNodes("//*[@x:Name='TitleBar']", $manager)[0].RemoveAttribute('MouseDown')
    $xamlReader = New-Object System.Xml.XmlNodeReader $xaml
    [Windows.Markup.XamlReader]::Load($xamlReader)
}


function Set-EventHandler {

    $CloseButton.add_Click({
        param([System.Object]$sender,[System.Windows.RoutedEventArgs]$e)
        CloseButton_Click($sender,$e)
    })
    $TitleBar.add_MouseDown({
        param([System.Object]$sender,[System.Windows.Input.MouseButtonEventArgs]$e)
        TitleBar_MouseDown($sender,$e)
    })
}


$window = Import-Xaml
Add-ControlVariables
Set-EventHandler




function CloseButton_Click
{
    param($sender, $e)
    $window.Close()
}


function TitleBar_MouseDown
{
    param($sender, $e)
    $window.DragMove()
}

$LogoImage.Source = "$PSScriptRoot/Images/Box-03.png"

$window.ShowDialog()

I just released new versions of the tools that now support resources. This should make it easier to include them by path without having to edit the PS1.

1 Like

@adam this worked, also worked with the font family where I reference a .tff file

<TextBlock Text="Lorem ipsum dolor sit amet. Est earum deserunt non beatae cupiditate et nesciunt voluptatem sed nulla repudiandae vel suscipit voluptatibus. Qui molestiae architecto rem obcaecati Quis et ipsam amet id quas saepe cum repudiandae internos non porro quibusdam et galisum unde. Id galisum praesentium quo numquam vitae et excepturi aperiam consequatur illo. Qui doloremque excepturi ut mollitia mollitia At quisquam voluptas eum dolorem numquam qui dolor asperiores est sapiente enim."
                       Foreground="White"
                       FontSize="12"
                       FontWeight="Medium"
                       FontFamily="Fonts/#Dosis"
                       TextWrapping="Wrap"
                           Margin="0,10,0,0"/>
1 Like