【问题标题】:How to select and use an image in Windows 8如何在 Windows 8 中选择和使用图像
【发布时间】:2014-02-09 10:47:57
【问题描述】:

我正在将我的 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();
}

但两者都不起作用。

【问题讨论】:

    标签: c# windows windows-8 photo


    【解决方案1】:

    给你:

    这比你想象的要复杂一些。

    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.";
                }
    
            }
        }
    }
    

    【讨论】:

    • 天哪!这就是我所需要的:)我什至可以复制粘贴:)谢谢:)
    【解决方案2】:

    我已经编写了这个代码来选择一个图像并在图像控件中显示它.. 它正在工作。

    public async Task<bool> pickPhoto()
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.List;
            openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            openPicker.FileTypeFilter.Add("*");
    
            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                BitmapImage image = new BitmapImage();
                image.SetSource(stream);
                imageChangedProfilePic.Source = image;
                imageChangedProfilePic.Stretch = Stretch.Fill;
                return true;
            }
            else
            {
                //  OutputTextBlock.Text = "Operation cancelled.";
                return false;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多