我想做一个powershell脚本,接受文件图标,并将它们转换为base64。有一个API程序集可以让我获得文件图标(不仅仅是关联的,还有图像缩略图和视频缩略图)。不幸的是,它给出的格式是一个BitmapSource。我想知道如何将源文件转换为图像,或者更准确地说,通过powershell将其转换为base64。
注意:它必须是powershell...
我试着做了大量的研究来寻找答案,但所有的事情都是用c#完成的,这使得我很难将c#转换成powershell。
这就是我到目前为止所知道的:
Add-Type -Path ..\dll\Microsoft.WindowsAPICodePack.Shell.dll
Add-Type -Path ..\dll\Microsoft.WindowsAPICodePack.dll
Import-Module ..\dll\Microsoft.WindowsAPICodePack.dll
Import-Module ..\dll\Microsoft.WindowsAPICodePack.Shell.dll
[System.Windows.Media.Imaging.BitmapSource] $iconSrc = [Microsoft.WindowsAPICodePack.Shell.ShellFile]::FromFilePath('image I am converting').Thumbnail.BitmapSource
编辑:
我找到了一种方法,可以使用以下代码将源文件转换为图像:
[System.Windows.Media.Imaging.BitmapSource] $iconSrc = [Microsoft.WindowsAPICodePack.Shell.ShellFile]::FromFilePath ('Image to convert path').Thumbnail.BitmapSource
$MemoryStream = New-Object System.IO.MemoryStream
$enc = New-Object System.Windows.Media.Imaging.BmpBitmapEncoder
$enc.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($iconSrc))
$enc.Save($MemoryStream)
$bitmap = New-Object System.Drawing.Bitmap($MemoryStream)
然而,我也需要将图像转换为base64,这是我有困难做的。我试图将图像保存到内存流中,但当我查看字节时,它变成了null。
MemoryStream = New-Object System.IO.MemoryStream
$bitmap.save($MemoryStream)
$Bytes = $MemoryStream.ToArray()
$MemoryStream.Flush()
$MemoryStream.Dispose()
$iconB64 = [convert]::ToBase64String($Bytes)
发布于 2017-04-24 16:10:44
$bitmap | gm定义显示需要第二个imageformat参数。
尝试将图像格式添加到位图保存。这似乎对我很有效:
MemoryStream = New-Object System.IO.MemoryStream
$bitmap.save($MemoryStream, [System.Drawing.Imaging.ImageFormat]::Jpeg)
$Bytes = $MemoryStream.ToArray()
$MemoryStream.Flush()
$MemoryStream.Dispose()
$iconB64 = [convert]::ToBase64String($Bytes)
发布于 2017-02-11 22:20:09
也许我误解了这里的问题,但将图像转换为Base64应该就像下面这样简单:
[convert]::ToBase64String((Get-Content $ImagePath -Encoding Byte))
再说一次,我可能误解了你的问题,但我已经用它在GUI中嵌入了blob,这样我就没有任何文件依赖关系了。
https://stackoverflow.com/questions/42177769
复制相似问题