C# 如何在Windows 8中选择和使用图像
我正在将我的Windows Phone 8应用程序移植到Windows 8,我需要选择一张图片并设置图片的来源!我今天必须把它搬走。我用了这个:C# 如何在Windows 8中选择和使用图像,c#,windows,windows-8,photo,C#,Windows,Windows 8,Photo,我正在将我的Windows Phone 8应用程序移植到Windows 8,我需要选择一张图片并设置图片的来源!我今天必须把它搬走。我用了这个: FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicke
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();
}
但是这两种方法都不起作用。我编写了这段代码,用于拾取图像并在图像控件中显示它。。 它正在工作
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;
}
}
public异步任务pickPhoto()
{
FileOpenPicker openPicker=新的FileOpenPicker();
openPicker.ViewMode=PickerViewMode.List;
openPicker.SuggestedStartLocation=PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(“*”);
StorageFile文件=等待openPicker.PickSingleFileAsync();
如果(文件!=null)
{
var stream=await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage=新的BitmapImage();
image.SetSource(流);
imageChangedProfilePic.Source=图像;
imageChangedProfilePic.Stretch=Stretch.Fill;
返回true;
}
其他的
{
//OutputExtBlock.Text=“操作已取消。”;
返回false;
}
}
我编写了此代码,用于拾取图像并在图像控件中显示。。
它正在工作
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;
}
}
public异步任务pickPhoto()
{
FileOpenPicker openPicker=新的FileOpenPicker();
openPicker.ViewMode=PickerViewMode.List;
openPicker.SuggestedStartLocation=PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(“*”);
StorageFile文件=等待openPicker.PickSingleFileAsync();
如果(文件!=null)
{
var stream=await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage=新的BitmapImage();
image.SetSource(流);
imageChangedProfilePic.Source=图像;
imageChangedProfilePic.Stretch=Stretch.Fill;
返回true;
}
其他的
{
//OutputExtBlock.Text=“操作已取消。”;
返回false;
}
}
给你:
这比你想象的要复杂一些
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.";
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.Storage;
使用Windows.Storage.Pickers;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Media.Imaging;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=234238
名称空间App1
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
受保护的覆盖异步无效OnNavigatedTo(NavigationEventArgs e)
{
FileOpenPicker opener=新的FileOpenPicker();
opener.ViewMode=PickerViewMode.缩略图;
opener.SuggestedStartLocation=PickerLocationId.PicturesLibrary;
opener.FileTypeFilter.Add(“.jpg”);
opener.FileTypeFilter.Add(“.jpeg”);
opener.FileTypeFilter.Add(“.png”);
StorageFile file=wait opener.PickSingleFileAsync();
如果(文件!=null)
{
//我们现在拿到文件了,处理一下。
var stream=await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage=new Windows.UI.Xaml.Media.Imaging.bitmapImage();
等待bitmapImage.SetSourceAsync(流);
bastin.Source=位图图像;
var decoder=wait Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(流);
}
其他的
{
//OutputExtBlock.Text=“该操作可能已被取消。”;
}
}
}
}
给你:
这比你想象的要复杂一些
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.";
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.Storage;
使用Windows.Storage.Pickers;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Media.Imaging;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=234238
名称空间App1
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
受保护的覆盖异步无效OnNavigatedTo(NavigationEventArgs e)
{
FileOpenPicker opener=新的FileOpenPicker();
opener.ViewMode=PickerViewMode.缩略图;
opener.SuggestedStartLocation=PickerLocationId.PicturesLibrary;
opener.FileTypeFilter.Add(“.jpg”);
opener.FileTypeFilter.Add(“.jpeg”);
opener.FileTypeFilter.Add(“.png”);
StorageFile file=wait opener.PickSingleFileAsync();
如果(文件!=null)
{
//我们现在拿到文件了,处理一下。
var stream=await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage=new Windows.UI.Xaml.Media.Imaging.bitmapImage();
等待bitmapImage.SetSourceAsync(流);
布斯丁,苏