Converting Text to Image

by Feb 18, 2019

WPF (Windows Presentation Foundation)is not just a technique to create UIs. You can create any type of vector-based graphics composition and save it to a graphics file.

Here is a simple example that takes any text and font, and renders it into a PNG file:

function Convert-TextToImage
    $Font = 'Consolas',
    $FontSize = 24,
    $Foreground = [System.Windows.Media.Brushes]::Black,
    $Background = [System.Windows.Media.Brushes]::White
  $filename = "$env:temp\$(Get-Random).png"

  # take a simple XAML template with some text  
  $xaml = @"

  Add-Type -AssemblyName PresentationFramework
  # turn it into a UIElement
  $reader = [XML.XMLReader]::Create([IO.StringReader]$XAML)
  $result = [Windows.Markup.XAMLReader]::Load($reader)
  # refine its properties
  $result.FontFamily = $Font
  $result.FontSize = $FontSize
  $result.Foreground = $Foreground
  $result.Background = $Background
  # render it in memory to the desired size
  $result.Measure([System.Windows.Size]::new([Double]::PositiveInfinity, [Double]::PositiveInfinity))
  # write it to a bitmap and save it as PNG
  $render = [System.Windows.Media.Imaging.RenderTargetBitmap]::new($result.ActualWidth, $result.ActualHeight, 96, 96, [System.Windows.Media.PixelFormats]::Default)
  Start-Sleep -Seconds 1
  $encoder = [System.Windows.Media.Imaging.PngBitmapEncoder]::new()
  $filestream = [System.IO.FileStream]::new($filename, [System.IO.FileMode]::Create)
  # clean up
  # return the file name for the generated image

Here is how to use:

PS> $file = Convert-TextToImage -Text 'Red Alert!' -Font Stencil -FontSize 60 -Foreground Red -Background Gray

PS> Invoke-Item -Path $file

Your learning points:

  • With XAML, an XML-based UI description language, you can define graphics
  • PowerShell can use the [Windows.Markup.XAMLReader] class to easily convert any valid XAML into a UIElement object.
  • UIElement objects can be saved as graphics file, for example as a PNG image, can be displayed as a window, or printed. In this example, we focused on saving to file, and we used a very simple XAML definition. Maybe you got curious now. Google for any of the methods used in this example, and you find plenty of reading. – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!