Use base64 for Images in WPF-based PowerShell Forms

I’m currently building a notification module to let me know when VMware vCenter alerts go off in my home lab. I plan to share it, and wanted to use a non-standard icon, but didn’t want to require a separate download. I knew base64 would be the answer, and ultimately, some C# code from StackExchange helped me figure out what needed to be done.

Here’s what it looks like in my application. Note the image being used in both the popup and the notifyicon itself:

icon

Below is the simplified code I used to display the icon within my application popup. The code contains comments that explains each step. It’s pretty straightforward: a quick conversion, then setting the image source to the converted stream.

# Add required assembly
Add-Type -AssemblyName PresentationFramework

# Setup the XAML
[xml]$script:xaml = '<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="base64 image demo" Height="200" Width="200" Background="Black">
    <Grid>
        <Image Name="thisistheimage" Height="32" Width="32"/>
    </Grid>
</Window>'

# Create the form and set variables
$script:form = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml))
$xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $form.FindName($_.Name) -Scope Script }

# here's the base64 string
$base64 = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7
           DAcdvqGQAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuNWWFMmUAAAJESURBVHhe7ZZRaiNRDAR9ON//St4JKIskikc3gwaBU1
           A/DQ9V/DHk9fl87rgFapPE0XAL1CaJo+EWqE0SR8MtUJskjoZboDZJHA0L7/f79YRxLtO7ZHE0LFDshHEu07tkcTQsUOyEcS7Tu2RxN
           CxQ7IRxLtO7ZHE03AK1SeJouAVqk8TRcAvUJomj4RaoTRJHwwJ9sCaMc5neJYujYYFiJ4xzmd4li6NhgWInjHOZ3iWLo2GBYieMc5ne
           JYuj4RaoTRJHwy1QmySOhlugNkkcDbdAbZI4GhbogzVhnMv0LlkcDQsUO2Gcy/QuWRwNCxQ7YZzL9C5ZHA0LFDthnMv0LlkcDbdAbZI
           4Gm6B2iRxNNwCtUniaLgFapPE0bBAH6wJ41ymd8niaFig2AnjXKZ3yeJoWKDYCeNcpnfJ4mhYoNgJ41ymd8niaLgFapPE0XAL1CaJo+
           EWqE0SR8MtUJskjoYF+mBNGOcyvUsWR8MCxU4Y5zK9SxZHwwLFThjnMr1LFkfDAsVOGOcyvUsWR8MtUJskjoZboDZJHA23QG2SOBpug
           dokcTTcArVJ4mi4BWqTxNFwC9QmiaPhFqhNEkfDLVCbJI6GW6A2SRwd6V/VJ6UmRxwdKepJqckRR0eKOnm9OUJvTl5PbomjI0WdvN4c
           oTcnrye3xNGRok5eb47Qm5PXk1vi6EhRJ683R+jNyevJLXF0pKiT15sj9Obk9eSWODpS1JNSkyOOjhT1pNTk2Idv4f/f/I1//C9/P8C
           PX/4DfF7/AHQpDe71ItxOAAAAAElFTkSuQmCC"

# Create a streaming image by streaming the base64 string to a bitmap streamsource
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($base64)
$bitmap.EndInit()

# Freeze() prevents memory leaks.
$bitmap.Freeze()

# Set source here. Take note in the XAML as to where the variable name was taken.
$thisistheimage.source = $bitmap

# Show form
$form.ShowDialog() | Out-Null

Working with PowerShell and WPF makes me realize that *this* is what I always wanted coding to be. Simplified yet powerful!

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in PowerShell, WPF
5 comments on “Use base64 for Images in WPF-based PowerShell Forms
  1. Jon says:

    Thanks so much for posting this! I was scouring the net looking for example code to load an embedded base64 image into an XAML form. I almost lost hope. Your solution was *exactly* what I was hoping to find, elegant and straightforward!

  2. Adam Schwartzberg says:

    Awesome solution – using this to drive the icon on the window for a script.

  3. Darryl says:

    Excellent – thank you. Still the only decent example solution out there!

  4. Jan Reinhoud says:

    I know this is an old post, but is it possible to use this to add an icon to powershell script with wpf gui? The script will be converted to exe, so i need the icon to be code.

    Thanks in advance

Leave a Reply