winutil/functions/private/convertTo-Bitmap.ps1

31 lines
897 B
PowerShell
Raw Normal View History

function ConvertTo-Bitmap {
<#
.SYNOPSIS
Converts an image file to a Bitmap object
.PARAMETER image
The path to the image file to convert
.EXAMPLE
2024-07-15 08:48:00 -05:00
ConvertTo-Bitmap -imageFilePath "C:\path\to\image.png"
#>
param (
2024-07-15 08:48:00 -05:00
$imageFilePath
)
# Read the image file as a byte array
2024-07-15 08:48:00 -05:00
$imageBytes = [System.IO.File]::ReadAllBytes($imageFilePath)
# Convert the byte array to a Base64 string
$base64String = [System.Convert]::ToBase64String($imageBytes)
# Create a streaming image by streaming the base64 string to a bitmap streamsource
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($base64String)
$bitmap.EndInit()
$bitmap.Freeze()
# Return the bitmap object
return $bitmap
}