Icon Color Replacement Fun with PowerShell

I like to keep interfaces simple and tend to use icons in my GUI designs. Recently, when making a PowerShell GUI to alert myself to alarm status changes in vCenter, I decided to reuse the same icon over and over for different statuses, but I made them meaningful by dynamically changing the color.

The Goal

My ultimate goal was to have 3 icons that were colored white, yellow and red.

white yellow red

How I did it

The first thing I did was search iconfinder for a free icon, and decided on this one:


Using paint.net, I determined that the base color of iconfinder’s server icon was #444444. Now what’s really cool about iconfinder, is they also offer the base64 code. As discussed in my earlier post, base64 can easily be used as icons and images in WPF forms.

So let’s take a look at what the code below executes to accomplish this task

  1. Loads WPF assemblies and sets base64 icon variable
  2. Creates a bitmapimage object to enable streaming of the base64 image
  3. Creates a colormap. Colormap is simple and just contains .OldColor and .NewColor.
  4. Passes colormap to an imageattribute which performs a SetRemapTable()
  5. Graphics.DrawImage draws an image based on these new imageattributes
  6. Saves the icon to variable for later use within the script ($iconwhite, $bmpwhite)
  7. Saves and opens the new icon so that you can confirm it worked.
  8. Do this for all colors within the newcolor array

This is high performance, and the total conversion time is less than 5 ms, which makes this technique efficient enough to use in all of your PowerShell GUI apps, if you’re so inclined :)

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in PowerShell, WPF

Leave a Reply

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


Migrating SQL Server?

dbatools is an awesome PowerShell module that helps you migrate entire instances with a single command.

Available from dbatools.io and github

SqlServer Needs You

SqlServer now has a dedicated engineer and Microsoft is asking for our input!

Upvote priorities and cmdlets now


Chrissy LeMaire

Brandon Abshire
View Brandon Abshire, MCDBA's profile on LinkedIn


Chrissy has been awarded the Microsoft MVP for her work in the PowerShell community.

Join us!

Belgian PowerShell
User Group

  SQL PASS PowerShell
Virtual User Group


Upvotes Needed

Help persuade Microsoft to open source SQL Server's PowerShell module, SQLPS.

Upvote now on Microsoft Connect