Created
March 4, 2024 05:05
-
-
Save HaroldMitts/210c6524b4cdd9ea4769375c9a5bfded to your computer and use it in GitHub Desktop.
This PowerShell script allows you to select an image file, converts it to a base64 string, and saves the string to a text file. It's useful for embedding images in text-based formats like HTML or CSS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Open a file dialog for the user to choose an image | |
Add-Type -AssemblyName System.Windows.Forms | |
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog | |
$OpenFileDialog.ShowDialog() | Out-Null | |
$image_path = $OpenFileDialog.FileName | |
# Read the image file and convert it to a base64 string | |
$ImageBytes = [System.IO.File]::ReadAllBytes($image_path) | |
$Base64Image = [System.Convert]::ToBase64String($ImageBytes) | |
# Open a save file dialog for the user to choose the save location and file name | |
$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog | |
$SaveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" | |
$SaveFileDialog.DefaultExt = "txt" | |
$SaveFileDialog.ShowDialog() | Out-Null | |
$save_path = $SaveFileDialog.FileName | |
# Write the base64 string to the chosen file | |
[System.IO.File]::WriteAllText($save_path, $Base64Image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just create a file and run it from the terminal. It will open a file browser so you can find your image file, then it will open another file browser so you can save a text file which will contain your base64 encoding. You can then copy and paste that encoding for use in your HTML, or wherever you want to use that base64 encoded image.