C# 使用文件选择器在图像UI元素中设置图片
在我的应用程序中,用户可以从设备内存(即平板电脑内存或桌面本地驱动器)设置配置文件pic,并将其上载到服务器。 我使用了文件选择器,这样用户可以选择一张图片并将其设置为配置文件图片,但问题是图片没有粘附到图像元素上。 我的代码: 我得到的文件路径是这个C# 使用文件选择器在图像UI元素中设置图片,c#,image,windows-8,microsoft-metro,filepicker,C#,Image,Windows 8,Microsoft Metro,Filepicker,在我的应用程序中,用户可以从设备内存(即平板电脑内存或桌面本地驱动器)设置配置文件pic,并将其上载到服务器。 我使用了文件选择器,这样用户可以选择一张图片并将其设置为配置文件图片,但问题是图片没有粘附到图像元素上。 我的代码: 我得到的文件路径是这个 filePath=C:\Users\Prateek\Pictures\IMG_0137.JPG 我不知道出了什么问题。我不确定这是否能解决问题,这就是我设置图像源所做的 使用位图图像作为图像的源 BitmapImage bitmapimage
filePath=C:\Users\Prateek\Pictures\IMG_0137.JPG
我不知道出了什么问题。我不确定这是否能解决问题,这就是我设置图像源所做的 使用位图图像作为图像的源
BitmapImage bitmapimage = new BitmapImage();
StorageFile file = await openPicker.PickSingleFileAsync();
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
await bitmapimage.SetSourceAsync(stream);
profilePicture.Source = bitmapImage;
我用过这个代码
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
this.textBlock.Text =
"File Path: " + file.Path + Environment.NewLine +
"File Name: " + file.Name;
try
{
var stream = await file.OpenReadAsync();
var imageSource = new BitmapImage();
await imageSource.SetSourceAsync(stream);
this.image.Source = imageSource;
}
catch (Exception ex)
{
this.textBlock.Text = ex.ToString();
}
}
else
{
this.textBlock.Text = "Operation cancelled.";
}
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
this.textBlock.Text =
"File Path: " + file.Path + Environment.NewLine +
"File Name: " + file.Name;
try
{
var stream = await file.OpenReadAsync();
var imageSource = new BitmapImage();
await imageSource.SetSourceAsync(stream);
this.image.Source = imageSource;
}
catch (Exception ex)
{
this.textBlock.Text = ex.ToString();
}
}
else
{
this.textBlock.Text = "Operation cancelled.";
}