尝试这个。下面的代码将在 TempFolder 中保存选择的音频文件的专辑封面
private async void btnPickFile_Click(object sender, RoutedEventArgs e)
{
string[] Music = new string[] { ".mp3", ".wma", ".m4a", ".aac" };
FileOpenPicker openPicker = new FileOpenPicker();
foreach (string extension in Music)
{
openPicker.FileTypeFilter.Add(extension);
}
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
await SaveThumbnail("MySongThumb.png", file);
}
}
private async Task SaveThumbnail(string ThumbnailName, StorageFile file)
{
if (file != null)
{
using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.MusicView, 100))
{
if (thumbnail != null && thumbnail.Type == ThumbnailType.Image)
{
var destinationFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(ThumbnailName, CreationCollisionOption.GenerateUniqueName);
Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(thumbnail.Size));
IBuffer iBuf = await thumbnail.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None);
using (var strm = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
{
await strm.WriteAsync(iBuf);
}
}
}
}
}
更新 1
private async Task<StorageFile> SaveThumbnail(StorageItemThumbnail objThumbnail)
{
if (objThumbnail != null && objThumbnail.Type == ThumbnailType.Image)
{
var picker = new FileSavePicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
picker.FileTypeChoices.Add("PNG Image", new string[] { ".png" });
StorageFile destinationFile = await picker.PickSaveFileAsync();
if (destinationFile != null)
{
Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(objThumbnail.Size));
IBuffer iBuf = await objThumbnail.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None);
using (var strm = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
{
await strm.WriteAsync(iBuf);
}
}
return destinationFile;
}
else
{
return null;
}
}