z. Appendix I : Base64 images - CodeBlueG/PSForms GitHub Wiki

In the form, I use base64 encoded images to create icons and images and I do this using the script below.

This script will take an input from a file in a standard image format and converts it to a text file that contains the image in base64 encoded format. I have tried this with .bmp, .png, .ico and .jpg files and they all have worked.

Before converting the file, I would recommend resizing the image to the size that you want on the form. Although you can force the image to a specific size within Windows Forms, I have found that the image is much clearer if it is correctly sized whilst it is an image and then converted to base64.

  $Folder = "C:\folder\Images"
  $FileName = "ImageFile.png"

  $Picture = Join-Path $Folder $FileName
  $OutputFileName = $FileName.Substring(0,$filename.length - 3) + "txt"
  $OutputFile = Join-Path $Folder $OutputFileName
  $Width = 100
  [byte[]]$Pic = Get-Content $Picture -Encoding Byte
  [system.convert]::ToBase64String($Pic) |
  ForEach-Object {$line = $_
      for ($i = 0; $i -lt $line.Length; $i += $Width){
          $length = [Math]::Min($Width, $line.Length - $i)
          $line.SubString($i, $length)
      }
  } | Set-Content $OutputFile  

I created this small script based on a few different articles I found on the web, I'm making no claim to this being my own!