如何在Windows 8.1 metro应用程序中上传图像?

时间:2014-03-26 13:25:41

标签: c# windows-8 microsoft-metro windows-8.1

在我的地铁应用中,我想上传image。实际上在button-click上,应该打开文件浏览器并提示图像。当用户选择任何图像时,我想使用view标记在image中显示它,并将其保存在服务器端。我有以下图片和按钮代码:

 <Border Margin="0,30,0,0" BorderThickness="2" BorderBrush="#FFAAA7A7" HorizontalAlignment="Center" Height="113">
            <Image Height="101" Source="temp.png" Margin="0,-2,0,10"/>
 </Border>
 <AppBarButton HorizontalAlignment="Center" Label="Upload Image" VerticalAlignment="Center" Icon="Camera" Width="178" Margin="0,0,0,0" Click="AppBarButton_Click" />

1 个答案:

答案 0 :(得分:1)

您正在搜索FilePicker对话框。 Here是关于它的好文章。以下代码执行:

  • 打开文件选择器对话框
  • 为图片类型添加过滤器
  • 将图像保存在StorageFile(应用程序的独立存储空间 - details

此代码取自页面:

if (rootPage.EnsureUnsnapped())
  {
  FileOpenPicker openPicker = new FileOpenPicker();
  openPicker.ViewMode = PickerViewMode.Thumbnail;
  openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
  openPicker.FileTypeFilter.Add(".jpg");
  openPicker.FileTypeFilter.Add(".jpeg");
  openPicker.FileTypeFilter.Add(".png");

  StorageFile file = await openPicker.PickSingleFileAsync();
  if (file != null)
  {
    // Application now has read/write access to the picked file
    //OutputTextBlock.Text = "Picked photo: " + file.Name;
  }
  else
  {
    //OutputTextBlock.Text = "Operation cancelled.";
  }
}