Start-Automating
Start-Automating

Reputation: 8367

How can I set album artwork in a Win8 C# application?

I'd like to be able to take an existing photo and set it as the album artwork.

I can use GetThumbnailAsync to give me a thumbnail and GetOutputStream to get the thumbnails output stream. Unfortunately, it's not writeable.

How might I set the album artwork (or any thumbnail) on an item in a Win8 C# application?

Current (non-working) code. It dies when the outStream is flushed with an Access Denied Error

    FileOpenPicker fileopenpicker = new FileOpenPicker();

    fileopenpicker.FileTypeFilter.Add(".jpg");
    fileopenpicker.FileTypeFilter.Add(".png");

    fileopenpicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;// | PickerLocationId.DocumentsLibrary | PickerLocationId.PicturesLibrary | PickerLocationId.MusicLibrary;

    var singlefileoperation = await fileopenpicker.PickSingleFileAsync();
    var read = await singlefileoperation.OpenAsync(FileAccessMode.Read);

    StorageFile replay = currentlyPlaying;
    TimeSpan pos = ME.Position;
    ME.Stop();

    //curren
    StorageItemThumbnail storageItemThumbnail = await currentlyPlaying.GetThumbnailAsync(ThumbnailMode.SingleItem);




    IOutputStream inputStreamAt = storageItemThumbnail.GetOutputStreamAt(0);
    Stream outStream = inputStreamAt.AsStreamForWrite();

    var inStreamAt = read.GetInputStreamAt(0);
    var inStream = inStreamAt.AsStreamForRead();
    await inStream.CopyToAsync(outStream);
    await outStream.FlushAsync();
    outStream.Dispose();
    inStream.Dispose();
    inStreamAt.Dispose();

Upvotes: 2

Views: 1322

Answers (2)

Stefan Fabian
Stefan Fabian

Reputation: 510

Just found out about this GitHub project that makes TagLib Sharp compatible with Windows 8+ Apps TagLib Sharp Portable. You can add it to your project via NuGet although its still a prerelease version.

However there is currently one bug using the included StreamFileAbstraction class. It sometimes throws a StackOverflowException when saving Tags or Album Art. With a custom FileAbstraction class it worked for me like a charm.

You can use it similiar to the original TagLib:

StorageFile file = [...]
TagLib.File tagFile = TagLib.File.Create(new FileAbstraction(file.Name, (await file.OpenAsync(FileAccessMode.ReadWrite)).AsStream()));

The rest stays like in JHixson's answer.

The custom FileAbstraction class:

public class FileAbstraction : TagLib.File.IFileAbstraction
{
    public FileAbstraction(string name, Stream stream)
    {
        this.Name = name;
        this.ReadStream = stream;
        this.WriteStream = stream;
    }

    public void CloseStream(Stream stream)
    {
        stream.Flush();
    }

    public string Name
    {
        get;
        private set;
    }

    public Stream ReadStream
    {
        get;
        private set;
    }

    public Stream WriteStream
    {
        get;
        private set;
    }
}

Upvotes: 0

JHixson
JHixson

Reputation: 1522

TagLib Sharp Should help with what you are looking for.

TagLib.File tagFile = TagLib.File.Create(c:/yourpath/yoursong.mp3);
IPicture newArt = new Picture(c:/yourimagepath/youralbumart.jpg);
tagFile.Tag.Pictures = new IPicture[1] {newArt};
tagFile.Save();

Code Source

Edit: Updated Link to TagLib Sharp Library

Upvotes: 0

Related Questions