locked
How set Image Source with C#

    Question

  • Hi,

    In C# I use FileOpenPicker to select the test.jpg, But it can't display the test.jpg.

    1. C#

    private async void button_addImage_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker fileOpenPicker = new FileOpenPicker();
        //fileOpenPicker.ViewMode = PickerViewMode.List;
        fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
        fileOpenPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
        fileOpenPicker.FileTypeFilter.Add(".jpg");
        fileOpenPicker.FileTypeFilter.Add(".jpeg");
        fileOpenPicker.FileTypeFilter.Add(".png");
    
        StorageFile file = await fileOpenPicker.PickSingleFileAsync();
        if (file != null)
        {
            Uri uri = new Uri(file.Path, UriKind.RelativeOrAbsolute);
            image_show.Source = new BitmapImage(uri);
            
            button_addImage.Visibility = Visibility.Collapsed;
        }
    }

    2. XAML

    <Grid Background="Orange" VerticalAlignment="Stretch"> <!-- Source="ms-appx:///Assets/test.jpg" this is OK --> <Image x:Name="image_show" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Uniform"/>

    <Button x:Name="button_addImage" Content="+" Click="button_addImage_Click"> </Grid>


    Monday, April 13, 2015 3:28 PM

Answers