nixda
nixda

Reputation: 2707

Convert a [System.Drawing.Bitmap] to [File.IFileAbstraction]

Task

In PowerShell, I want to add a picture as ID3V2 tag with the help of taglip-sharp library.
Usually you do it this way:

# Load taglib-sharb library
[Reflection.Assembly]::LoadFrom( (Resolve-Path "..\Common\taglib-sharp.dll"))

# Load MP3 file
$media = [TagLib.File]::Create("C:\MusicFile.mp3")

# Load picture
$pic = [taglib.picture]::createfrompath("C:\MyAlbumArt.jpg")

# Add picture to MP3
$media.Tag.Pictures = $pic

# Save Mp3 
$media.Save() 

General problem

The above works only if your picture is a file located somewhere locally. But I have a picture as [System.Drawing.Bitmap] object which has no filepath to load it with createfrompath.

Before you say Then just write it to disc: I'd rather avoid saving them since my final script processes many pictures and mp3 files. And I don't want to write and delete hundreds of pictures.

Specific question

I see that taglib also has another method called CreateFromFile(File.IFileAbstraction ) to load a picture. But how do I convert a [System.Drawing.Bitmap] into a IFileAbstraction?

The closest help I found on the net was here and here. But it's for .NET 4.5 and I am by no means able to convert this into PowerShell syntax

Here is the documentation of taglib-sharp's picture object if it helps.

Upvotes: 2

Views: 2660

Answers (1)

Fabian
Fabian

Reputation: 2086

The easy version is to just call the c# code in powershell (as described here)

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

The full powershell version:

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

Tested in powershell v3

Upvotes: 2

Related Questions