将字节数组转换为字符串值



我试图返回图像的属性,似乎无法弄清楚如何将PropertyItems值转换为字符串

$img = [System.Drawing.Image]::FromFile('C:tempChart01.jpg')
$img.PropertyItems |
ForEach-Object {
Write-Host $_.Id, $_.Value, @{ Expression={[System.Text.Encoding]::ASCII.GetString( -ExpandProperty $_.Value )} }
}

有人可以帮助这个工作,并解释一下我哪里错了。我相信这很容易(只要你知道怎么做)。

另外,是否有任何方法将属性的编号转换为命名值?

提前谢谢你。

更新我继续尝试这个和那个,并试图在网上找到这个(我敢肯定我不是第一个尝试这样做的人!),并取得了一些进展,但仍然停留在许多属性类型

$img.PropertyItems | Sort-Object Id | Select-Object -Property Id, Type, Value, @{name="V"; expression={ [System.Text.Encoding]::ASCII.GetString($_.Value)}}
ForEach-Object {
#https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.propertyitem.type?view=windowsdesktop-5.0
<#if($_.Type -eq 1){ #array of bytes.
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF8Encoding).GetString($_.V)} }
}#>
if($_.Type -eq 2){ #null-terminated ASCII string.
#[System.Text.ASCIIEncoding]::ASCII.GetString( $img.GetPropertyItem(271).Value )
#Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF8Encoding).GetString($_.V)} }
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF8Encoding).GetString($_.V)} }
}
if($_.Type -eq 3){ #array of unsigned short (16-bit) integers
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UnicodeEncoding).GetString($_.V)} }
}
if($_.Type -eq 4){ #array of unsigned long (32-bit) integers.
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF32Encoding).GetString($_.V)} }
}
<#if($_.Type -eq 5){ #array of pairs of unsigned long integers. Each pair represents a fraction; the first integer is the numerator and the second integer is the denominator.
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF32Encoding).GetString($_.V)} }
}
if($_.Type -eq 6){ #array of bytes that can hold values of any data type.
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF32Encoding).GetString($_.V)} }
}
if($_.Type -eq 7){ #array of signed long (32-bit) integers.
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF32Encoding).GetString($_.V)} }
}
if($_.Type -eq 10){ #array of pairs of signed long integers. Each pair represents a fraction; the first integer is the numerator and the second integer is the denominator.
Write-Host $_.Id, $_.Value, @{ Expression={(new-object System.Text.UTF32Encoding).GetString($_.V)} }
}#>
}

我不确定这是否是你要找的:

$img = [System.Drawing.Image]::FromFile('pathtoimg.jpg')
$enc = [Text.Encoding]::UTF8
$img.PropertyItems | Select-Object *,@{n='Value';e={$enc.GetString($_.Value)}} -ExcludeProperty Value
Id  Len Type Value                                                                                                                                                     
--  --- ---- -----                                                                                                                                                     
256    2    3 5                                                                                                                                                        
257    2    3 �                                                                                                                                                        
258    6    3                                                                                                                                                        
262    2    3                                                                                                                                                          
274    2    3                                                                                                                                                          
277    2    3                                                                                                                                                          
282    8    5   H                                                                                                                                                      
283    8    5   H                                                                                                                                                      
296    2    3                                                                                                                                                          
305   34    2 Adobe Photoshop CC 2017 (Windows)                                                                                                                         
306   20    2 2017:08:21 18:50:01                                                                                                                                       
36864    4    7 0221                                                                                                                                                      
40961    2    3 ��                                                                                                                                                        
40962    4    4 5                                                                                                                                                        
40963    4    4 �                                                                                                                                                        
20507 5724    1 ���� Adobe_CM �� Adobe d�   �� �            ...                                                                                                      
20515    2    3                                                                                                                                                          
20525    8    5 H                                                                                                                                                        
20526    8    5 H                                                                                                                                                        
20528    2    3                                                                                                                                                          
513    4    4 �                                                                                                                                                        
514    4    4                                                                                                                                                         
20625  128    3                                                                                           
20624  128    3                                   

关于"是否有任何方法将属性的编号转换为命名值?">,我会说是的,您可以在Select-Object上使用hashtable和另一个计算属性。如果你编辑你的答案显示一个表与IdNamed Value,我可以编辑这个答案显示给你。

最新更新