我希望能够拍摄现有照片并将其设置为专辑图片。
我可以使用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();
答案 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();
编辑:更新了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;
}
}