Loading a Base64-encoded Picture into WPF Window

by Jan 6, 2016

In a previous tip we illustrated how you can convert any picture into a Base64-encoded string. Today, we'd like to show how you can load a picture from a Base64-encoded string and display it.

The example code creates a WPF window inviting you to the upcoming PowerShell Conference EU 2016 taking place in April 2016 (where you can attend roughly 80 sessions and discuss PowerShell topics with more than 30 prominent speakers. More info can be found at www.psconf.eu, but do not wait for too long because seats are limited).

The window displays the official conference logo yet does not require a separate picture file. Instead, the image is embedded into the script and loaded directly into memory.

The embedded picture at the beginning of the script is pretty large. The key part is the function Convert-Base64Bitmap2Picture below the Base64-encoded string. It accepts the Base64-encoded text and returns a bitmap object. This object can then be set as the source of any WPF image control.

Add-Type -AssemblyName PresentationFramework

$picture1 ='/9j/4AAQSkZJRgABAAEBLAEsAAD/4QB2RXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAA
AZKGAAcAAABAAAAALAAAAABVTklDT0RFAABMAEUAQQBEACAAVABlAGMAaABuAG8AbABvAGcAaQBl
AHMAIABJAG4AYwAuACAAVgAxAC4AMAAxAAD/2wBDAAUFBQgFCAwHBwwMCQkJDA0MDAwMDQ0NDQ0N
DQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ3/2wBDAQUICAoHCgwHBwwNDAoM
DQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ3/wAARCADI
AMgDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIE
AwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJico
KSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZ
mqKjpKWmp6ipqrKztLW2t7i5usLDxMX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fLOg5PoJV4Eg9zhx2btXfQx
U8PotYfyv9H0/LyOGvhYYjV+7PpJfquv5+Z88694avvDcvl3iYRjhJV5ik/3W7H1RsMPTHNfT0a8
K6vTevVPRr5fqtD5etQnh3aa06NbP+uz1MGuk5QoAKACgAoAKAPX/gxqX2TWHtCcLdwsAPV4/wB4
p/BBIPxrwc1p81BVFvCS+6Wj/Gx9DlFTkxDp9Jxf3x1X4XOL8c3n23Xb6X0uJEH0iPlj9EFehhI8
mHpR/uJ/+Be9+p5uMlz4mrL++18o+7+hyldxwBQAUAewfB//AI+rz/rjF/6MNeHmPw0/8T/I97Lf
iqei/M91r5w+iPm/4pf8hw/9e8P8mr6rAfwf+3pHymP/AI3/AG7E86r1TyiWCeS1kWaBmikjOVdC
VZT6gjkf5zUtKScZJNPdPYqMnBqUW01s1pY9r8L/ABQWTba63hG4C3SjCn/rsg+6fV0G3+8o614F
fANXnh9v5Ov/AG6+
    55
    55087
    2102
    
    PowerShell provides seven different streams you can use to output information. Streams help sort out information because streams can be muted. In fact, some streams are muted by default. Here is a sample function called Test-Stream. When you run it, it sends information to all seven streams.

Please note: Write-Information was added in PowerShell 5.0. Remove the call to Write-Information from the sample if you want to run it in older PowerShell versions!

function Test-Stream
{
    #region These are all the same and define return values
    'Return Value 1'
    echo 'Return Value 2'
    'Return Value 3' | Write-Output
    #endregion
    
    Write-Verbose 'Additional Information'
   Write-Debug 'Developer Information'
   Write-Host 'Mandatory User Information'
   Write-Warning 'Warning Information'
   Write-Error 'Error Information'

   # new in PowerShell 5.0
   Write-Information 'Auxiliary Information' 
}

Here is what you will most likely see when you run Test-Stream:

 
PS C:\> Test-Stream
Return Value 1
Return Value 2
Return Value 3
Mandatory User  Information
WARNING: Warning  Information
Test-Stream : Error  Information
At line:1 char:1
+ Test-Stream
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error]]>
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-Stream 
 
PS C:\> $result =  Test-Stream
Mandatory User  Information
WARNING: Warning  Information
Test-Stream : Error  Information
At line:1 char:1
+ Test-Stream
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error],  WriteErrorException
    + FullyQualifiedErrorId :  Microsoft.PowerShell.Commands.WriteErrorException,Test-Stream 
 
PS C:\> $result
Return Value 1
Return Value 2
Return Value 3
 
PS C:\>
 

As you can see, echo and Write-Output work the same, and in fact are the same (because echo is an alias for Write-Output). They define the return value(s). They can be assigned to a variable. The same is true for unassigned values that your function leaves behind: they go to Write-Output as well.

Write-Host sends text output directly to the console, so it is guaranteed to be always visible. This cmdlet should be used for messages to the user.

The remaining streams are silent. To see the output from the remaining streams, you need to enable them first:

$VerbosePreference = 'Continue'
$DebugPreference = 'Continue'
$InformationPreference = 'Continue'

Once you did this, Test-Stream produces output like this:

 
PS C:\> Test-Stream
Return Value 1
Return Value 2
Return Value 3
VERBOSE: Additional Information
DEBUG: Developer Information
Mandatory User Information
Auxiliary Information 
 

To restore the default values, reset the preference variables:

$VerbosePreference = 'SilentlyContinue'
$DebugPreference = 'SilentlyContinue'
$InformationPreference = 'SilentlyContinue'

If you add the common parameters to your function, you can use -Verbose and -Debug when you call the function. Test-CommonParameter illustrates how you add common parameter support.

function Test-CommonParameter
{
    [CmdletBinding()]
    param()
    
    "VerbosePreference = $VerbosePreference"
    "DebugPreference = $DebugPreference"
}

When you run Test-CommonParameter, you will immediately understand how the -Verbose and -Debug common parameters work: they simply change the local preference variables:

 
PS C:\> Test-CommonParameter
VerbosePreference = SilentlyContinue
DebugPreference = SilentlyContinue

PS C:\> Test-CommonParameters -Debug -Verbose
VerbosePreference = Continue
DebugPreference = Inquire 
 

Twitter This Tip! ReTweet this Tip!