• 28、FileThumbnails


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

     
    //用于 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 像素长宽比: 统一,宽长宽比大约 .7
            PicturesView = 0,
            //
            // 摘要:
            //     显示视频文件预览。默认值,首选的大小: 中等,最好至少为 190 x 130 像素长宽比: 统一,宽长宽比大约 .7
            VideosView = 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 file
                    FileOpenPicker 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 document
                    FileOpenPicker 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 folder
                    FolderPicker 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/p/2705478.html
Copyright © 2020-2023  润新知