从相机胶卷中的照片设置Image.Source

时间:2015-06-19 12:57:49

标签: c# image xaml windows-phone-8.1 camera-roll

在我的Windows Phone 8.1 App中,我用相机拍照并将其保存在相机胶卷中,并将图像路径保存在临时对象中:

var picture = library.SavePictureToCameraRoll(fileName, e.ImageStream);
geophoto.ImagePath = picture.GetPath();

在我的应用程序的另一页面中,我想从相机胶卷加载此照片,并将保存的路径设置为Image对象的来源:

Uri uri = new Uri(App.Current.Geophoto.ImagePath, UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
this.ShutterImage.Source = imgSource; 

图像的保存路径例如是"file:///C:/Data/Users/Public/Pictures/Camera Roll/201506191442443805.jpg"

在运行时,当我尝试设置新来源时,图像变为空白。路径或代码有问题吗?

1 个答案:

答案 0 :(得分:0)

我发现我无法通过路径直接访问相机。所以我用以下代码解决了我的问题:

    private BitmapImage GetThumbnailFromCameraRoll(string path)
    {
        MediaLibrary mediaLibrary = new MediaLibrary();
        var pictures = mediaLibrary.Pictures;
        foreach (var picture in pictures)
        {
            var camerarollPath = picture.GetPath();
            if (camerarollPath == path)
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(picture.GetThumbnail());
                return image;
            }
        }

        return null;
    }