Skip to content

Instantly share code, notes, and snippets.

@hatsunea
Created November 27, 2020 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatsunea/9856c22a23b42b124aa883e69d03b5b3 to your computer and use it in GitHub Desktop.
Save hatsunea/9856c22a23b42b124aa883e69d03b5b3 to your computer and use it in GitHub Desktop.
using ImageFilePickerSample.Common;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
namespace ImageFilePickerSample.Models
{
public class TItem : INotifyPropertyChanged
{
private Guid _ID;
public Guid ID
{
get { return this._ID; }
set
{
if (this._ID != value)
{
this._ID = value;
OnPropertyChanged();
}
}
}
private ImageSource _ImageItem;
public ImageSource ImageItem
{
get { return _ImageItem; }
set
{
if (_ImageItem != value)
{
_ImageItem = value;
OnPropertyChanged();
}
}
}
private async Task SetImageFromFileAsync()
{
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");
var file = await picker.PickSingleFileAsync();
var imageFile = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(file.Name);
using (var stream = await imageFile.OpenReadAsync())
{
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bitmapImage.SetSource(stream);
this.ImageItem = bitmapImage;
}
}
private RelayCommand _GetImageCommand;
public RelayCommand GetImageCommand
{
get
{
if (_GetImageCommand == null)
{
_GetImageCommand = new RelayCommand(async () =>
{
await this.SetImageFromFileAsync();
});
}
return _GetImageCommand;
}
set
{
_GetImageCommand = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment