下面示例中将要使用的公共类:

//用于 picker 选择文件时,进行文件过滤
internal class FileExtensions{public static readonly string[] Document = new string[] { ".doc", ".xls", ".ppt", ".docx", ".xlsx", ".pptx", ".pdf", ".txt", ".rtf" };public static readonly string[] Image    = new string[] { ".jpg", ".png", ".bmp", ".gif", ".tif" };public static readonly string[] Music    = new string[] { ".mp3", ".wma", ".m4a", ".aac" };}//错误提示internal class Errors{public static readonly string NoExifThumbnail   = "No result (no EXIF thumbnail or cached thumbnail available for fast retrieval)";public static readonly string NoThumbnail       = "No result (no thumbnail could be obtained from the selected file)";public static readonly string NoAlbumArt        = "No result (no album art available for this song)";public static readonly string NoIcon            = "No result (no icon available for this document type)";public static readonly string NoImages          = "No result (no thumbnail could be obtained from the selected                                                                                 folder - make sure that the folder contains images)";public static readonly string FileGroupEmpty    = "No result (unexpected error: retrieved file group was null)";public static readonly string FileGroupLocation = "File groups are only available for library locations, please select                                                                                  a folder from one of your libraries";public static readonly string Cancel            = "No result (operation cancelled, no item selected)";}

显示获得缩略图的结果信息的方法:

 public static void DisplayResult(Image image, TextBlock textBlock, string thumbnailModeName,uint size, IStorageItem item, StorageItemThumbnail thumbnail, bool isGroup){// ErrorMessage.Visibility = Visibility.Collapsed;BitmapImage bitmapImage = new BitmapImage();bitmapImage.SetSource(thumbnail);image.Source = bitmapImage;textBlock.Text = String.Format("ThumbnailMode.{0}\n"+ "{1} used: {2}\n"+ "Requested size: {3}\n"+ "Returned size: {4}x{5}",thumbnailModeName,isGroup ? "Group" : item.IsOfType(StorageItemTypes.File) ? "File" : "Folder",item.Name,size,thumbnail.OriginalWidth,thumbnail.OriginalHeight);}

Windows.Storage.FileProperties 命名空间下,系统缩略图的枚举类型 :

View Code

   // 摘要://     描述缩略图的目的确定如何调整缩略图象检索。[Version(100794368)]public enum ThumbnailMode{// 摘要://     显示一个图片文件预览。默认值,首选的大小: 中等,最好至少为 190 x 130 像素长宽比: 统一,宽长宽比大约 .7PicturesView = 0,//// 摘要://     显示视频文件预览。默认值,首选的大小: 中等,最好至少为 190 x 130 像素长宽比: 统一,宽长宽比大约 .7VideosView = 1,//// 摘要://     显示音乐文件预览。默认值,首选的大小: 小,最好至少为 40 x 40 像素长宽比: 统一,这样长宽比MusicView = 2,//// 摘要://     若要显示的文档文件的预览。默认值,首选的大小: 小,最好至少为 40 x 40 像素长宽比: 统一,这样长宽比DocumentsView = 3,//// 摘要://     若要显示列表中的文件 (或其他项目) 的预览。默认设置首选的大小: 小,最好是至少 40 x 40 像素长宽比: 统一,这样长宽比ListView = 4,//// 摘要://     要显示的任何单个的项目 (如文件、 文件夹或文件组) 的预览。默认值,首选的大小: 大,在最长端的至少 256 个像素长宽比: 变量,使用文件的原始长宽比SingleItem = 5,}

1、Display a thumbnail for a picture :

为图片创建相应的缩略图,并且根据它的类型优化它。

操作截图 :

请点击下面的按钮选择一个文件,并获得缩略图:

下拉框展开后,选择缩略图的类型 :

点击 “Get Thumbnail” :

点击打开,显示结果 :

页面相应的 xaml :

 <CheckBox x:Name="FastThumbnailCheckBox" ><TextBlock Text="Return fast thumbnail only (could be lower quality)" TextWrapping="Wrap" Style="{StaticResource BasicTextStyle}" /></CheckBox><ComboBox x:Name="ModeComboBox"><ComboBoxItem x:Name="PicturesView" IsSelected="true">Thumbnail for a grid layout</ComboBoxItem><ComboBoxItem x:Name="ListView">Thumbnail for a list layout</ComboBoxItem><ComboBoxItem x:Name="SingleItem">Thumbnail for a single layout</ComboBoxItem></ComboBox><Button x:Name="GetThumbnailButton" Content="Get Thumbnail" Margin="0,10,0,0" Click="GetThumbnailButton_Click"/>

相应的 按钮事件 :

   private async void GetThumbnailButton_Click(object sender, RoutedEventArgs e){// 选择图片FileOpenPicker openPicker = new FileOpenPicker();//添加图片过滤类型foreach (string extension in FileExtensions.Image){openPicker.FileTypeFilter.Add(extension);}StorageFile file = await openPicker.PickSingleFileAsync();if (file != null){string thumbnailModeName = ((ComboBoxItem)ModeComboBox.SelectedItem).Name;                 //描述缩略图的目的确定如何调整缩略图象检索。ThumbnailMode thumbnailMode = (ThumbnailMode)Enum.Parse(typeof(ThumbnailMode), thumbnailModeName);bool fastThumbnail = FastThumbnailCheckBox.IsChecked.Value;ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale; // 默认基于显示的每英寸像素 (PPI),增加请求的大小。if (fastThumbnail){thumbnailOptions |= ThumbnailOptions.ReturnOnlyIfCached; //仅当进行缓存或嵌入文件时检索缩略图。}const uint size = 200;                 // 针对文件检索调整的缩略图图像,这取决于缩略图的用途、要求的大小以及指定的选项。       using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(thumbnailMode, size, thumbnailOptions)){if (thumbnail != null){// 方法在文章最开始处DisplayResult(ThumbnailImage, OutputTextBlock, thumbnailModeName, size, file, thumbnail, false);}else if (fastThumbnail){// 错误 : Errors.NoExifThumbnail
                        }else{// 错误 : Errors.NoThumbnail
                        }}}else{// 错误 : Errors.Cancel
                }}

2、Display a album art for a song :

获得一个音乐文件的缩略图。单击下面的按钮,选择一个音乐文件。

操作截图 :

单击按钮,弹出 Picker,选择一个音乐文件 :

显示结果 (如果选择一个没有缩略图的音乐文件,则没有显示内容) :

相应的 xaml :

  <Button x:Name="GetThumbnailButton"  Content="Get Thumbnail"  Click="GetThumbnailButton_Click"/>

<Image x:Name="ThumbnailImage" Stretch="None"  /><TextBlock x:Name="OutputTextBlock" />

相应的 C# :

 private async void GetThumbnailButton_Click(object sender, RoutedEventArgs e){// Pick a music fileFileOpenPicker openPicker = new FileOpenPicker();foreach (string extension in FileExtensions.Music){openPicker.FileTypeFilter.Add(extension);}StorageFile file = await openPicker.PickSingleFileAsync();if (file != null){
                    // 显示音乐文件预览。默认值,首选的大小: 小,最好至少为 40 x 40 像素长宽比: 统一,这样长宽比const ThumbnailMode thumbnailMode = ThumbnailMode.MusicView;const uint size = 100;using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(thumbnailMode, size)){// Also verify the type is ThumbnailType.Image (album art) instead of ThumbnailType.Icon// (which may be returned as a fallback if the file does not provide album art)if (thumbnail != null && thumbnail.Type == ThumbnailType.Image){// 显示缩略图DisplayResult(ThumbnailImage, OutputTextBlock, thumbnailMode.ToString(), size, file, thumbnail, false);}else{//错误 : Errors.NoAlbumArt
                        }}}else{//错误 :  Errors.Cancel
                }}

3、Display an icon for a document :

获得文档的缩略图 。

操作截图 :

单击按钮 :

选择 word 文档,点击 "打开" 显示结果 :

页面的 xaml :

 <Button x:Name="GetThumbnailButton"  Content="Get Thumbnail" Click="GetThumbnailButton_Click"/>

<Image x:Name="ThumbnailImage" Stretch="None"  /><TextBlock x:Name="OutputTextBlock" />

相应的 C# :

       private async void GetThumbnailButton_Click(object sender, RoutedEventArgs e){// Pick a documentFileOpenPicker openPicker = new FileOpenPicker();foreach (string extension in FileExtensions.Document){openPicker.FileTypeFilter.Add(extension);}StorageFile file = await openPicker.PickSingleFileAsync();if (file != null){                 //若要显示的文档文件的预览。默认值,首选的大小: 小,最好至少为 40 x 40 像素长宽比: 统一,这样长宽比const ThumbnailMode thumbnailMode = ThumbnailMode.DocumentsView;const uint size = 100;using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(thumbnailMode, size)){if (thumbnail != null){DisplayResult(ThumbnailImage, OutputTextBlock, thumbnailMode.ToString(), size, file, thumbnail, false);}else{//错误 : Errors.NoIcon
                        }}}else{//错误 : Errors.Cancel
                }}

4、Display a thumbnail for a folder :

和上面的类似,只需把上面两行代码改为 :

StorageFolder folder = await folderPicker.PickSingleFolderAsync();

//显示一个图片文件预览。默认值,首选的大小: 中等,最好至少为 190 x 130 像素长宽比: 统一,宽长宽比大约 .7
const ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView;

5、Display a thumbnail for a file group :

获取一组图片的缩略图,根据月份排列。

请点击下面的按钮来选择一个文件夹,获得缩略图。文件组仅为库文件夹,所以确保你选择一个库的位置。

单击按钮 :

点击确定,显示结果 :

页面的 xaml  :

<Button x:Name="GetThumbnailButton" Content="Get Thumbnail" Click="GetThumbnailButton_Click"/>

 <Image x:Name="ThumbnailImage"  Stretch="None"  /><TextBlock x:Name="OutputTextBlock" /><TextBlock x:Name="OutputDetails" />

相应的 C#  :

 private async void GetThumbnailButton_Click(object sender, RoutedEventArgs e){// Pick a folderFolderPicker folderPicker = new FolderPicker();foreach (string extension in FileExtensions.Image){folderPicker.FileTypeFilter.Add(extension);}              // 显示 folderPicker 对象,以便用户可以选择文件夹。StorageFolder folder = await folderPicker.PickSingleFolderAsync();if (folder != null){                  //描述用于在查询结果中分组文件的条件,并确定来自子文件夹的文件是否包含在结果中。const CommonFolderQuery monthShape = CommonFolderQuery.GroupByMonth;//基于每个文件的 System.ItemDate 属性按月份生成虚拟文件夹。// Verify queries are supported because they are not supported in all picked locations.
                 //检索指示当前文件夹是否支持指定公共文件夹查询的值。if (folder.IsCommonFolderQuerySupported(monthShape)){// Convert folder to file group and query for items                     // 创建用于执行针对当前文件夹的子存储文件夹的筛选搜索查询的对象。根据所指定的文件夹属性,分组搜索结果。IReadOnlyList<StorageFolder> months = await folder.CreateFolderQuery(monthShape).GetFoldersAsync();if (months != null && months.Count > 0){const ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView;const uint size = 200;StorageFolder firstMonth = months[0];using (StorageItemThumbnail thumbnail = await firstMonth.GetThumbnailAsync(thumbnailMode, size)){if (thumbnail != null){DisplayResult(ThumbnailImage, OutputTextBlock, thumbnailMode.ToString(), size, firstMonth, thumbnail, true);// Also display the hierarchy of the file group to better visualize where the thumbnail comes from
                                // 基于通用文件查询获取当前文件夹中的文件。IReadOnlyList<StorageFile> files = await firstMonth.GetFilesAsync();  if (files != null){StringBuilder output = new StringBuilder("\nList of files in this group:\n\n");foreach (StorageFile file in files){output.AppendFormat("{0}\n", file.Name);}OutputDetails.Text = output.ToString();}}else{//错误 : Errors.NoImages
                                }}}else{//错误 : Errors.FileGroupEmpty
                        }}else{//错误 :  Errors.FileGroupLocation,
                    }}else{//错误 : Errors.Cancel
                }}

转载于:https://www.cnblogs.com/hebeiDGL/archive/2012/09/27/2705478.html

28、FileThumbnails相关推荐

  1. 【PyTorch教程】P27、28、29 完整的模型套路

    P27.28.29 完整的模型套路 一步一步跟着做笔记: 查看数据集的长度: 把写好的这部分,单独放在一个模块里,起名叫做model: 这个模块,稍加改动,比如添加import,再用一个测试的torc ...

  2. 【MAPBOX基础功能】28、mapbox地图基础工具 -获取当前地图中心点

    前言 官网指引,生成accesstoken,下载相关依赖请翻阅[https://blog.csdn.net/weixin_44402694/article/details/125414381?spm= ...

  3. 28、查询男生、女生人数(不重点)(自己做出)

    -- 28.查询男生.女生人数(不重点) SELECT s_sex, count(distinct s_id) FROM student GROUP BY s_sex

  4. 第 27、28、29 节 接口、抽象类、SOLID、单元测试、反射

    第27.28.29节 接口.抽象类.SOLID.单元测试.反射 什么是接口和抽象类 SOLID 接口 依赖反转原则和单元测试 什么是接口和抽象类 1)接口和抽象类都是"软件工程产物" ...

  5. 练习7.28、7.29、7.30

    7.28:如果move.set和display函数的返回类型不是Screen&而是Screen,则在上一个练习中将会发生什么情况? Ans:若返回类型不是引用,则三个函数返回的都是对象的副本, ...

  6. Android Java(2015-6-18 15:28、2016-1-30 21:18、2016-5-31 11:20)

    20190729添加: HsqdeMac:javaalgorithm hsq$ java -version java version "12.0.2" 2019-07-16 Jav ...

  7. 【学习笔记】28、类的方法及参数介绍

    类的方法及参数介绍 初始化方法 在类被实例化的时候,有一个方法会被自动运行,这个方法就是"__init__(self)" (前后两个下划线). 这个方法主要用于在一个对象被实例化的 ...

  8. 28、Power Query-分支语句的妙用

    本节重点:认识Power Query里面的分支语句,并且介绍简单的运用. 例子:下图是一个班级学生的考试成绩,比如我们约定下面的规则,统计优良中差的人数. 优  >=250 良  >=22 ...

  9. 28、OSPF配置实验之负载均衡

    1.负载均衡解析 OSPF只支持等价负载均衡,默认为4条线路负载均衡,最大支持6条线路负载均衡,我们可以在OSPF路由进程下通过maximum-paths 6修改OSPF支持的负载均衡线路数. 2.实 ...

最新文章

  1. java不会自动提示_eclispe中打点不会提示的解决方法,以及自动补全
  2. 成功进行微博营销的几点技巧
  3. [MIPS汇编语言]对于数的输入和输出
  4. python把一堆图片分成n份,用Python一键生成炫酷九宫格图片,火了朋友圈
  5. Fence Repair POJ - 3253
  6. 社会化分享实现插件,分享到...
  7. 程序员如何与人打交道
  8. LeetCode 2201. 统计可以提取的工件(哈希)
  9. servlet3.0注解loadOnStartup不起作用解决方案
  10. java oss如何下载文件_Java OSS批量下载并压缩为ZIP代码实例
  11. Linux万兆网络配置
  12. ps画画模糊笔刷_如何用笔刷做出大神级效果?1000多款PS插画笔刷,简直就是你想要的神器...
  13. 时间管理——帕累托法则(二八定律)
  14. Misc_PNG高宽详解
  15. python数据分析师前景及待遇怎么样_数据分析师未来五年发展前景怎么样?
  16. 汉诺塔(hanio)
  17. 计算机电源指示灯量一下就灭,开机硬盘指示灯闪了一下就不亮是怎么回事
  18. python入门指南
  19. python程序段的基本结构_python3 第三章 - 程序的基本结构
  20. python.exe: No module named pip以及pycharm下载插件失败等问题

热门文章

  1. python网络爬虫的基本步骤-Python网络爬虫与信息提取(一)(入门篇)
  2. python小游戏代码大全-python贪吃蛇游戏代码
  3. 关于python中requests模块导入问题-python引入requests模块报错
  4. c+和python先学哪个比较好-C和Python我该先学什么?
  5. python数字类型-python数字类型
  6. python画函数曲线-使用Python画数学函数曲线
  7. python手册中文版apk-Go、Python 语言解析安卓 APK 包
  8. python100个必背知识-python学习必备知识汇总
  9. python学哪个版本-老男孩python学习用的哪个版本?
  10. python与excel表格-如何用python处理excel表格