通过Powershell将BitmapSource转换为BitmapImage(或Base64)

时间:2017-02-11 15:42:23

标签: c# powershell bitmap

我想创建一个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。我试图将图像保存到内存流,但是当我查看字节时它变为空。

MemoryStream = New-Object System.IO.MemoryStream
$bitmap.save($MemoryStream)
$Bytes = $MemoryStream.ToArray()
$MemoryStream.Flush()
$MemoryStream.Dispose()
$iconB64 = [convert]::ToBase64String($Bytes)

2 个答案:

答案 0 :(得分:1)

$ 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)

答案 1 :(得分:0)

也许我在这里误解了这个问题,但是将图像转换为Base64应该就这么简单:

__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);

同样,我可能误解了你的问题,但我已经用它来嵌入GUI中的blob,这样我就不会有任何文件依赖。

相关问题