2

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();
Start-Automating
  • 8,067
  • 2
  • 28
  • 47
  • http://stackoverflow.com/questions/12924390/the-uri-given-to-mediacontrol-for-a-music-files-album-art-net – Freelancer Apr 16 '13 at 05:33
  • That's displaying existing album artwork for the currently playing track: Not setting the album artwork into the file itself. – Start-Automating Apr 17 '13 at 05:21
  • Some code you are using ? or the specific error you are getting ? – Danpe Apr 20 '13 at 14:12
  • Where are you getting the artwork from ? local or online ? Post the code your working on! – danielovich Apr 22 '13 at 11:06
  • Which file format are you targeting? For MP3 you have to use ID3-Tags, like described in [here](http://stackoverflow.com/questions/349581/mp3-cover-art-tagging-in-c-sharp) – Carsten Apr 24 '13 at 08:48
  • I will update the question with my code. I cannot target a particular format (building a player atop the MediaElement), but most of my personal collection is MP3 or M4A. The library you've described will not work, as it isn't supported in metro. – Start-Automating Apr 24 '13 at 09:06

2 Answers2

0

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

Community
  • 1
  • 1
JHixson
  • 1,512
  • 2
  • 14
  • 29
  • Absolutely not what I'm looking for. Specifically, I've noted repeatedly that this needs to work in a Win8 Metro app, which means no TagLib Sharp. – Start-Automating Apr 25 '13 at 16:58
  • Ah, my apologies. I looked into itfor a while myself. From the looks of it, your best bet might be adapting or updating the code found in the TagLib Sharp source. As you can see, they manually have to serialize and append the tag information to the file binary stream. It isn't exactly surprising that the Windows Store API doesn't have this feature, given that the Core .Net Library doesn't seem to have it either. From my cursory scanning of the TagLib Sharp source, it looks possible to adapt to the updated API. It would be a project, however. – JHixson Apr 26 '13 at 02:01
  • Yeah, that looks like the road I'll have to walk. I will re-answer after I've re-engineered something useful. – Start-Automating Apr 26 '13 at 17:49
  • Did you figure it out? I could adjust the library to allow me to set the album cover. However taglib sharp uses a ton of APIs that are not allowed in Windows Store Apps. – Stefan Fabian Aug 15 '14 at 15:02
0

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;
    }
}
Stefan Fabian
  • 498
  • 4
  • 21