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

//用于 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. cytocape绘图(三)
  2. 从mysql的官网下载tar.gz结尾的mysql
  3. 基于Jquery的颜色选择器
  4. [转]给定单链表,检测是否有环。如果有环,则求出进入环的第一个节点
  5. Matplotlib学习笔记1
  6. 如何用python做模型_python的模型如何使用?
  7. ubuntu14.04 安装 pyv8
  8. 九个你必须掌握的Java新手基础项目
  9. html5漂亮的登录与注册界面设计,翻转式用户登录注册界面设计
  10. 理解JESD204B链路参数 Understanding JESD204B Link Parameters
  11. bzoj 3375: [Usaco2004 Mar]Paranoid Cows 发疯的奶牛
  12. python爬虫——百度文本审核技术
  13. 麒麟芯片鸿蒙芯片高通骁龙,麒麟和骁龙两款处理器,选哪一款比较好,看完这三点你就明白了...
  14. imageJ二次开发之旅 – imageJ开源项目介绍
  15. 产品设计过程中的沉没成本和禀赋效应
  16. Eclipse插件 JSDT
  17. Java 8 之实战篇
  18. windows中的一些小技巧
  19. matlab用eval函数,matlab—eval函数
  20. 基于C#的WinForm打印程序附带源代码

热门文章

  1. python写出的程序如何给别人使用-如何用PYTHON代码写出音乐
  2. python计算每月工资-python税后工资计算器
  3. python简单代码编写-python读写Excel表格的实例代码(简单实用)
  4. python 创建txt文件并写入字符串-Python将字节字符串写入文件
  5. python中一共有多少个关键字-Python之33个关键字是哪些
  6. python可以写桌面软件吗-python能写桌面程序吗
  7. python程序在安卓上如何运行-在 android 上运行 python 的方法
  8. python单词意思-python 前面几个单词含义
  9. python代码格式-pyhon代码设计格式指南
  10. python一次性读取整个文件-python – 如何一次读取和附加到文本文件?