【问题标题】:WinJS / WinRT: detect corrupt image fileWinJS / WinRT:检测损坏的图像文件
【发布时间】:2012-10-03 17:06:23
【问题描述】:

我正在构建一个从本地图片库加载图片的 Win8/WinJS 应用程序。加载有效图像并在列表视图中显示它们通常一切正常。

现在我需要检测损坏的图像并为这些图像禁用部分应用程序。

例如,打开一个文本文件并在其中输入一些文本。将文件另存为 .jpg,这显然不是有效的 jpg 图像。由于 .jpg 名称,我的应用仍会加载文件,但现在我需要禁用应用的某些部分,因为图像已损坏。

有没有一种方法可以检查我加载的给定图像是否是有效的图像文件?检查它是否损坏?

我正在使用标准 WinRT / WinJS 对象,如 StorageFile、Windows.Storage.Search 相关对象等,根据文件类型搜索加载我的图像列表。

我不需要从搜索结果中过滤掉损坏的图像。我只需要能够在有人在 ListView 中选择图像后判断图像是否损坏。

【问题讨论】:

    标签: windows-8 windows-runtime winjs storagefile


    【解决方案1】:

    一种可能的解决方案是检查图像的widthheight 属性以确定它是否有效。

    【讨论】:

      【解决方案2】:

      是的,contentType 属性将返回任何文件扩展名。我能找到的最好方法是查看image properties

      file.properties.getImagePropertiesAsync()
                     .done(function(imageProps) {
                         if(imageProps.width === 0 && imageProps.height === 0) {
                             // I'm probably? likely? invalid.
                         });
      

      【讨论】:

        【解决方案3】:

        其中 SelectImagePlaceholder 是一个图像控件.. =)

        存储文件文件;

                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    try
                    {
                        // Set the image source to the selected bitmap
                        BitmapImage bitmapImage = new BitmapImage();
        
                        await bitmapImage.SetSourceAsync(fileStream);
        
        
                        SelectImagePlaceholder.Source = bitmapImage;
                        //SelectImagePlaceholder.HorizontalAlignment = HorizontalAlignment.Center;
                        //SelectImagePlaceholder.Stretch = Stretch.None;
                        this.SelectImagePlaceholder.DataContext = file;
        
                        _curMedia = file;
                    }
                    catch (Exception ex)
                    {
                        //code Handle the corrupted or invalid image
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-13
          • 2016-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-31
          相关资源
          最近更新 更多