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:


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.

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 and holds a master's degree in Systems Engineering. Chrissy is also 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

Your email address will not be published. Required fields are marked *