【问题标题】:upload photo using the FileOpenPicker windows8.1使用FileOpenPicker windows8.1上传照片
【发布时间】:2014-08-04 17:37:23
【问题描述】:

我正在尝试使用 FileOpenPicker 将照片上传到我的应用程序,当我编写以下代码时:

FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;

// Filter to include a sample subset of file types
open.FileTypeFilter.Clear();
open.FileTypeFilter.Add(".bmp");
open.FileTypeFilter.Add(".png");
open.FileTypeFilter.Add(".jpeg");
open.FileTypeFilter.Add(".jpg");

// Open a stream for the selected file
StorageFile file = await open.PickSingleFileAsync();

// ImageSource im = (new Uri (file.Path));
ChildPic.Source = new BitmapImage(new Uri(file.Path));

它没有给我一个错误,但图像控件留空。

路径中有值:C:\Users\Pictures\New folder(15).jpg

【问题讨论】:

  • ChildPic.Source = new BitmapImage(new Uri(file.Path));处打断点检查是否有路径值。让我们知道它给了什么......
  • 路径中有值:C:\Users\Pictures\New folder(15).jpg

标签: c# image microsoft-metro winrt-xaml windows-8.1


【解决方案1】:

new BitmapImage(Uri) 不适用于任何路径。它仅支持具有协议httpms-appxms-appdataURI。你必须使用流。

FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;

// Filter to include a sample subset of file types
open.FileTypeFilter.Clear();
open.FileTypeFilter.Add(".bmp");
open.FileTypeFilter.Add(".png");
open.FileTypeFilter.Add(".jpeg");
open.FileTypeFilter.Add(".jpg");

// Open a stream for the selected file
StorageFile file = await open.PickSingleFileAsync();

// ImageSource im = (new Uri (file.Path));
var bmp = new BitmapImage();
using (var strm = await file.OpenReadAsync())
{
    bmp.SetSource(strm);
    ChildPic.Source = bmp;
}

【讨论】:

    猜你喜欢
    • 2014-12-10
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多