如何使用Powershell将UTF-8二进制编码的图片转换为PNG格式
PowerShell UTF-8二进制编码图片转PNG实现方案
代码实现
场景1:已持有UTF-8编码的图片字符串变量
# 将$utf8ImageStr替换为你实际使用的UTF-8编码图片字符串变量 $imageBytes = [System.Text.Encoding]::UTF8.GetBytes($utf8ImageStr) [System.IO.File]::WriteAllBytes("output.png", $imageBytes)
场景2:UTF-8编码的图片内容存储在本地文本文件中
# 将input_utf8_image.txt替换为源文件路径,output.png替换为目标PNG文件路径 $utf8ImageStr = Get-Content -Path "input_utf8_image.txt" -Encoding UTF8 -Raw $imageBytes = [System.Text.Encoding]::UTF8.GetBytes($utf8ImageStr) [System.IO.File]::WriteAllBytes("output.png", $imageBytes)
注意事项
- 如果转换后PNG文件无法正常打开,可优先校验源UTF-8编码数据的完整性,排除源数据本身损坏、编码不匹配的问题
- 若源数据不是UTF-8编码,可将代码中的
UTF8替换为对应编码(如ASCII/Unicode等)即可适配
内容的提问来源于stack exchange,提问作者Raulus
相关产品推荐
相关产品推荐

