question

ikanchev avatar image
0 Votes"
ikanchev asked ikanchev answered

Powershell WPF Form background image

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-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ikanchev avatar image
0 Votes"
ikanchev answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.