我想能够采取现有的照片,并将其设置为专辑的艺术作品。
我可以使用GetThumbnailAsync
给我一个缩略图和GetOutputStream
来获得缩略图输出流。不幸的是,它是不可写的。
如何在Win8 c#应用程序中设置项目上的专辑图稿(或任何缩略图)?
当前(非工作)代码。当输出流被拒绝访问错误
刷新时,它将死亡。 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();
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();
代码来源
编辑:更新链接到TagLib Sharp库
刚刚发现了这个GitHub项目,使TagLib Sharp兼容Windows 8+ Apps TagLib Sharp Portable。你可以通过NuGet将它添加到你的项目中,尽管它仍然是一个预发布版本。
然而,目前有一个bug使用包括StreamFileAbstraction
类。当保存标签或专辑封面时,它有时会抛出StackOverflowException
。有了一个自定义的FileAbstraction
类,它对我来说就像一个魅力。
您可以像使用原始TagLib一样使用它:
StorageFile file = [...]
TagLib.File tagFile = TagLib.File.Create(new FileAbstraction(file.Name, (await file.OpenAsync(FileAccessMode.ReadWrite)).AsStream()));
其余的和JHixson的回答一样。
自定义fileabstract类:
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;
}
}