在 Windows 10 移动版中读取图像时出错
Posted
技术标签:
【中文标题】在 Windows 10 移动版中读取图像时出错【英文标题】:Error reading a image in Windows 10 Mobile 【发布时间】:2016-08-08 19:02:23 【问题描述】:我在选择图像后遇到了一个简单的图像库问题,我将它发送到 Windows 10 中的另一个屏幕,它运行良好,但是当我在手机中尝试时,它会无缘无故地崩溃,我的代码如下一:
XAML 代码:
<Page
x:Class="Stop_Diabetes.Pages.Camera"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Stop_Diabetes.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="#227EC2" Loaded="Page_Loaded">
<Page.Resources>
<local:ImageConverter x:Key="imageConverter"/>
<DataTemplate x:Key="imageTemplate">
<Grid Width="190" Height="130">
<Image Source="Binding Path=Thumbnail,
Converter=StaticResource imageConverter" Tapped="Image_Tapped"
Width="200" Height="200"/>
</Grid>
</DataTemplate>
<CollectionViewSource
x:Name="picturesSource"/>
</Page.Resources>
<Grid>
<Grid x:Name="stkGallery" Canvas.ZIndex="15" VerticalAlignment="Bottom">
<GridView x:Name="gvPictures" VerticalAlignment="Top" Height="150" ItemsSource="Binding Source=StaticResource picturesSource" ItemTemplate="StaticResource imageTemplate" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Vertical"></WrapGrid>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</Grid>
</Grid>
</Page>
C#重要代码:
private void Page_Loaded(object sender, RoutedEventArgs e)
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".jpg");
fileTypeFilter.Add(".jpeg");
fileTypeFilter.Add(".png");
fileTypeFilter.Add(".gif");
fileTypeFilter.Add(".bmp");
//Define thr query to iterate thriugh all the subfolders
var pictureQueryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter);
//Read through all the subfolders.
pictureQueryOptions.FolderDepth = FolderDepth.Deep;
//Apply the query on the PicturesLibrary
var pictureQuery = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(pictureQueryOptions);
//
var picturesInformation = new FileInformationFactory(pictureQuery, ThumbnailMode.PicturesView);
picturesSource.Source = picturesInformation.GetVirtualizedFilesVector();
private async void Image_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
Image img = (Image)sender;
FileInformation fi = img.DataContext as FileInformation;
if (fi != null)
string path = fi.Name;
//do something with the path...
var pStorage = KnownFolders.PicturesLibrary;
StorageFile file = await pStorage.GetFileAsync(path);
ImageProperties imgProp = await file.Properties.GetImagePropertiesAsync();
using (var imgStream = await file.OpenAsync(FileAccessMode.Read))
WriteableBitmap bitmap = new WriteableBitmap((int)imgProp.Width, (int)imgProp.Height);
bitmap.SetSource(imgStream);
Frame.Navigate(typeof(Crop), bitmap);
public class ImageConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, string culture)
if (value != null)
var img = (IRandomAccessStream)value;
var picture = new BitmapImage();
picture.SetSource(img);
return picture;
return DependencyProperty.UnsetValue;
public object ConvertBack(object value, Type targetType, object parameter, string culture)
throw new NotImplementedException();
我的问题发生在这里:
StorageFile 文件 = 等待 pStorage.GetFileAsync(path);
PC 可以完美打开图像,但是当我打电话时它显示:
Message = "系统找不到指定的文件。\r\n"
这很奇怪,因为图像已加载但稍后无法打开,我尝试了不同的选项,如 ID、路径等,但没有一个有效。有谁知道我应该在 Windows 10 移动版中进行哪些更改?
这或多或少应该是当前代码的结果:
【问题讨论】:
【参考方案1】:fileinfo 类有一个 openAsync 和 OpenReadAsync 方法,为什么不使用它们呢?
https://msdn.microsoft.com/en-us/library/windows/apps/br207562
https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.bulkaccess.fileinformation.openreadasync
这是您应该更改的代码
using (var imgStream = await fi.OpenReadAsync())
【讨论】:
我已经在这里使用它了: using (var imgStream = await file.OpenAsync(FileAccessMode.Read)) WriteableBitmap bitmap = new WriteableBitmap((int)imgProp.Width, (int)imgProp.高度); bitmap.SetSource(imgStream); Frame.Navigate(typeof(Crop), bitmap); 但您仍然需要指定您尝试打开的文件。以上是关于在 Windows 10 移动版中读取图像时出错的主要内容,如果未能解决你的问题,请参考以下文章
当使用 OpenCV 将 B/W 图像读取为 Numpy 数组时,我得到的结果与读取为图像不同
如何在 Windows 10 开发者预览版中启用 Bash?