简单的版本是在 powershell 中调用c# 代码(如此处所述)
$refs = @(
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Drawing.dll",
"d:\taglib-sharp.dll"
)
$Source = @"
namespace YourNamespace
{
public class Helper
{
public static TagLib.IPicture[] GetPictureFromBitmap(System.Drawing.Bitmap img)
{
return
new TagLib.IPicture[]
{
new TagLib.Picture(
new TagLib.ByteVector(
(byte[]) new System.Drawing.ImageConverter().ConvertTo(img, typeof (byte[]))))
};
}
}
}
"@
Add-Type -Path $refs
Add-Type -ReferencedAssemblies $refs -TypeDefinition $Source -Language CSharp
# Load MP3 file
$media = [TagLib.File]::Create("C:\MusicFile.mp3")
# Load picture into System.Drawing.Image
[Bitmap]$pic =[System.Drawing.Image]::FromFile("C:\MyAlbumArt.jpg")
# Add picture to MP3
$media.Tag.Pictures = [YourNamespace.Helper]::GetPictureFromBitmap($pic)
# Save Mp3
$media.Save()
完整的 powershell 版本:
function GetPictureFromBitmap([System.Drawing.Bitmap]$bitmap)
{
$converter = New-Object -TypeName System.Drawing.ImageConverter
$byte_vec = New-Object -TypeName TagLib.ByteVector -ArgumentList $converter.ConvertTo($bitmap, [byte[]])
$picture = New-Object -TypeName TagLib.Picture -ArgumentList $byte_vec
$picture_list = New-Object TagLib.IPicture[] 1
$picture_list[0] = $picture
return $picture_list
}
$refs = @(
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Drawing.dll",
"d:\taglib-sharp.dll"
)
Add-Type -Path $refs
# Load MP3 file
$media = [TagLib.File]::Create("C:\MusicFile.mp3")
# Load picture into System.Drawing.Image
[Bitmap]$pic = [System.Drawing.Image]::FromFile("C:\MyAlbumArt.jpg")
# Add picture to MP3
$media.Tag.Pictures = GetPictureFromBitmap($pic)
# Save Mp3
$media.Save()
在 powershell v3 中测试