Tony
Tony

Reputation: 31

UWP Set BitmapImage for Image Control

I am using Prism library to implement bind Image with BitmapImage.

This is xaml

<Button Grid.Row="1" Text="Resize Img" Command="{Binding ImageResize}"></Button>
<Image  Grid.Row="2" Source="{Binding ImageResult}"></Image>
<Image  Grid.Row="3" Source="{Binding ImageUri}"></Image>
<Label Text="{Binding ImageUri}"></Label>

This is ImagePageViewModel

public class ImagePageViewModel : BindableBase
{
    public ImagePageViewModel()
    {

    }
    private string _imageUri;
    public string ImageUri
    {
        get { return _imageUri; }
        set { SetProperty(ref _imageUri, value); }
    }
    private BitmapImage _imageResult;
    public BitmapImage ImageResult
    {
        get { return _imageResult; }
        set { SetProperty(ref _imageResult, value); }
    }

    private DelegateCommand _imageSelect;
    public DelegateCommand ImageSelect =>
        _imageSelect ?? (_imageSelect = new DelegateCommand(ExecuteImageSelect));

    async void ExecuteImageSelect()
    {
        try
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker
            {
                ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
                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)
            {
                using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    // Set the image source to the selected bitmap 
                    BitmapImage bitmapImage = new BitmapImage();
                    //bitmapImage.DecodePixelWidth = 600; //match the target Image.Width, not shown
                    await bitmapImage.SetSourceAsync(fileStream);
                    ImageResult = bitmapImage;
                    ImageUri = file.Path;
                    //ImageUri = @"http://pic26.nipic.com/20121221/9252150_142515375000_2.jpg";
                }
            }
            else
            {
                ImageUri = "";
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }        
}

For above code, setting ImageResult and setting ImageUri with file.Path both are not work. The file.Path return the right value for the file.

It only works when using with network file.

Is this related with File access permission if you browser the local disk file?

I assume this is related with the UWP File permission, but any idea how to resolve it?

Upvotes: 1

Views: 473

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

The way that setting ImageResult works well for UWP platform. But set image source with file.Path will not work. UWP run in sand box, we could not access file with path in the xaml. I check your code, it does not look like uwp project. because UWP does not contain Label control. If the project is xamarin, please refer xamarin image document

Update

The Froms project is shared library, you should not call UWP FileOpenPicker in it. For your requirement, you could create Dependency Service for Xamarin and convert Bitmap to Xamarin image source for more please refer this case.

Upvotes: 1

Related Questions