我想获得存储在“video”文件夹中的文件的缩略图,并将它们保存为本地文件夹中的图像。 这里是我的代码来获取文件。
var v = await KnownFolders.VideosLibrary.GetFilesAsync(); foreach (var file in v) { var thumb = await file.GetScaledImageAsThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem); BitmapImage Img = new BitmapImage(); Img.SetSource(thumb); await ApplicationData.Current.LocalFolder.CreateFolderAsync("VideoThumb"); var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync( "VideoThumb\\" + file.Name, CreationCollisionOption.FailIfExists); var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite); //I don't know how to save thumbnail on this file ! }
我的项目是Windows Phone 8.1运行时C#应用程序。
有几件事你需要处理:
file.Name
创建图像不是一个好主意,因此那些视频和它们的扩展名可能是mp4 / mpg /其他不是jpg / png / other , 我认为下面的代码应该做的工作:
private async Task SaveViedoThumbnails() { IBuffer buf; StorageFolder videoFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("VideoThumb", CreationCollisionOption.OpenIfExists); Windows.Storage.Streams.Buffer inputBuffer = new Windows.Storage.Streams.Buffer(1024); var v = await KnownFolders.VideosLibrary.GetFilesAsync(); foreach (var file in v) { var thumb = await file.GetScaledImageAsThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem); var imageFile = await videoFolder.CreateFileAsync(file.DisplayName + ".jpg", CreationCollisionOption.ReplaceExisting); using (var destFileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite)) while ((buf = (await thumb.ReadAsync(inputBuffer, inputBuffer.Capacity, Windows.Storage.Streams.InputStreamOptions.None))).Length > 0) await destFileStream.WriteAsync(buf); } }