2012-10-03 37 views
2

我正在构建一个从本地图片库加载图片的Win8/WinJS应用程序。一切正常工作正常加载有效的图像,并在列表视图中显示它们。WinJS/WinRT:检测损坏的图像文件

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

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

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

我使用像StorageFile,Windows.Storage.Search相关对象等标准WinRT/WinJS对象来加载基于搜索文件类型的图像列表。

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

回答

1

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

0

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

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

其中SelectImagePlaceholder是图像控制.. =)

StorageFile文件;

 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 
      } 
     }