Windows 8 WinJS/WinRT:检测损坏的图像文件

Windows 8 WinJS/WinRT:检测损坏的图像文件,windows-8,windows-runtime,winjs,storagefile,Windows 8,Windows Runtime,Winjs,Storagefile,我正在构建一个Win8/WinJS应用程序,从本地图片库加载图片。在加载有效图像并在列表视图中显示它们时,一切正常 现在我需要检测损坏的图像,并为这些图像禁用部分应用程序 例如,打开一个文本文件并在其中输入一些文本。将文件另存为.jpg,这显然不是有效的jpg图像。由于.jpg名称,我的应用程序仍然加载该文件,但现在我需要禁用应用程序的某些部分,因为图像已损坏 是否有一种方法可以检查我加载的给定图像是否是有效的图像文件?检查它是否损坏 我使用标准的WinRT/WinJS对象,如StorageFi

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

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

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

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

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


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

一个可能的解决方案是检查图像的
宽度
高度
属性,以确定其是否有效。

是的,
内容类型
属性将返回任何文件扩展名。我能找到的最好方法是查看:


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