Powershell WPF Form background image

Ivan Kanchev 121 Reputation points
2021-11-05T11:00:13.703+00:00

Hi all,

So i am building this simple GUI with PS WPF.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName 'Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="Notification" Name="Notification" Height="480" Width="680" Visibility="Visible" WindowStartupLocation="CenterScreen" Topmost="True" ResizeMode="NoResize" BorderThickness="1" WindowStyle="None" RenderTransformOrigin="0.5,0.5" AllowsTransparency="True" Background="Transparent">

    <Window.Resources>

    </Window.Resources>

    <Border Background="White" BorderBrush="Gray" BorderThickness="1,1,1,1" CornerRadius="6,6,6,6">
        <Grid>
            <Label Name="labelMessage"  HorizontalAlignment="Center" Margin="5,150,0,0" VerticalAlignment="Top" Height="60" Width="480" FontFamily="Metropolis" FontSize="14" Visibility="Visible">
                <AccessText TextWrapping="WrapWithOverflow">
                _Another long piece of text that requires text wrapping goes here.
                </AccessText>
            </Label>

        </Grid>
    </Border>
</Window>
'@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)

try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

$Form.ShowDialog() | out-null

What i am struggling at the moment is adding background image to that form. I have tested with multiple different ways to do so and it fails every time:

  • i tried:
    <Window.Resources>
    
    <ImageSource x:Key="imgSource">image_resource.jpg</ImageSource>
    <ImageBrush x:Key="backgroundImage" ImageSource="{DynamicResource imgSource}" Opacity="0.3"></ImageBrush>
    
    </Window.Resources>
    
  • I also tried:
       <Grid.Background>
    
            <ImageBrush ImageSource="Images/background.jpg"/>
    
        </Grid.Background>
    

Nothing seems to work as expected. I will appreciate if someone can point me in the correct direction for this issue.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ivan Kanchev 121 Reputation points
    2021-11-05T11:46:14.58+00:00

    After some more investigation i was able to solve the issue the following way:
    $uri = new-object system.uri("C:\background.jpg")
    $imagesource = new-object System.Windows.Media.Imaging.BitmapImage $uri
    $imagebrush = new-object System.Windows.Media.ImageBrush $imagesource
    $MyBorder.Background = $imagebrush

    So i added name to the Border Tag - MyBorder. From there i am crating the brush and imaging objects and assigning the brush to the border.

    0 comments No comments

0 additional answers

Sort by: Most helpful