c# - 如何在 Windows 8 中选择和使用图像

标签 c# windows windows-8 photo

我正在将我的 Windows Phone 8 应用程序移植到 Windows 8,我需要选择一张图片并为​​其设置图片源!我今天必须移植它。我用过这个:

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

Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();

if (file != null)
{
    BitmapImage img = new BitmapImage() { UriSource = new Uri((file.Path + file.DisplayName + file.FileType), UriKind.RelativeOrAbsolute) };
    image.Source = img;
}
else
{
}

它选择了图片,但没有改变来源,我还使用了:

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

Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();

if (file != null)
{
    BitmapImage img = new BitmapImage() { UriSource = new Uri(file.Path, UriKind.RelativeOrAbsolute) };
    bustin.Source = img;
    dt.Start();
    oyunsure.Start();
}
else
{
    dt.Start();
    oyunsure.Start();
}

但两者都不起作用。

最佳答案

给你:

这比您预期的要复杂一些。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            FileOpenPicker opener = new FileOpenPicker();
            opener.ViewMode = PickerViewMode.Thumbnail;
            opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            opener.FileTypeFilter.Add(".jpg");
            opener.FileTypeFilter.Add(".jpeg");
            opener.FileTypeFilter.Add(".png");

            StorageFile file = await opener.PickSingleFileAsync();
            if (file != null)
            {
                // We've now got the file. Do something with it.
                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                await bitmapImage.SetSourceAsync(stream);
                bustin.Source = bitmapImage;
                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
            }
            else
            {
                //OutputTextBlock.Text = "The operation may have been cancelled.";
            }

        }
    }
}

关于c# - 如何在 Windows 8 中选择和使用图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21658086/

相关文章:

windows - 如何通过 PowerShell 脚本将文件从 Windows 复制到 Ubuntu

c# - 与 Windows 命名管道 (.Net) 的异步双向通信

c# - 使用 InlineUIContainer 在 RichTextBlock 中不需要的重叠插入 UI 对象

c# - Windows Metro 应用程序中缺少 XPath

c# - 当属性名称是动态的时使用 ExpandoObject,这可能吗?

c# - Azure 存储 - 403 禁止访问

windows - 使用变量重命名文件

Android 模拟器和 AVD 无法在 Surface Pro 上运行

c# - 如何创建应用程序的 WPF 交互式导览

c# - ADO.NET:无法连接到 mdf 数据库文件