Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JPG to base64 and back [duplicate]

Tags:

powershell

I can convert a jpeg to a base64 string using the following PowerShell command

[Convert]::ToBase64String((Get-Content -Path .\Capture.jpg -Encoding Byte)) >> capture.txt

I tried converting it back using the following

[Convert]::FromBase64String((Get-Content -Path .\capture.txt)) >> capture2.jpg

But I get a list of numbers and not a binary file. How do I convert the base64 file back to binary?

like image 892
user3213700 Avatar asked Sep 04 '25 03:09

user3213700


1 Answers

If you want to handle it as a image you might want to rebuild it in memory (use it or make modifications) and then save it, like:

$Base64 = Get-Content -Raw -Path .\capture.txt
$Image = [Drawing.Bitmap]::FromStream([IO.MemoryStream][Convert]::FromBase64String($Base64))
$Image.Save("<path>\Image2.jpg")
like image 86
iRon Avatar answered Sep 05 '25 20:09

iRon