如何在Win8 C#应用程序中设置专辑图片?

时间:2013-04-16 05:31:02

标签: c# windows-8 albumart

我希望能够拍摄现有照片并将其设置为专辑图片。

我可以使用GetThumbnailAsync为我提供缩略图,GetOutputStream来获取缩略图输出流。不幸的是,它不可写。

如何在Win8 C#应用程序中的项目上设置相册图片(或任何缩略图)?

当前(非工作)代码。当使用Access Denied Error

刷新outStream时它会死掉
    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();

2 个答案:

答案 0 :(得分:0)

TagLib Sharp应该帮助你找到你想要的东西。

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

编辑:更新了TagLib Sharp Library的链接

答案 1 :(得分:0)

刚刚发现这个GitHub项目使TagLib Sharp与Windows 8+ Apps TagLib Sharp Portable兼容。您可以通过NuGet将其添加到项目中,尽管它仍然是预发布版本。

但是目前有一个使用包含的StreamFileAbstraction类的错误。 保存标签或专辑封面时,它有时会抛出StackOverflowException。 通过自定义FileAbstraction课程,它对我来说就像一个魅力。

您可以使用它与原始TagLib类似:

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

剩下的就像JHixson的回答一样。

自定义FileAbstraction类:

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;
    }
}