使用文件选择器在图像UI元素中设置图片

本文关键字:元素 设置 UI 图像 文件 选择器 | 更新日期: 2023-09-27 18:14:41

在我的应用程序中,用户可以从设备内存即平板电脑内存或桌面本地驱动器设置个人资料pic并将其上传到服务器。我使用文件选择器,以便用户可以选择一张图片并将其设置为个人资料图片,但问题是图片不坚持图像元素。我的代码:

 private async void filePicker()
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                String filePath = file.Path;
                System.Diagnostics.Debug.WriteLine(filePath);
                Uri uri = new Uri(filePath, UriKind.Relative);
                profilePicture.Source = new BitmapImage(uri);
            }
        }
        internal bool EnsureUnsnapped()
        {
            // FilePicker APIs will not work if the application is in a snapped state.
            // If an app wants to show a FilePicker while snapped, it must attempt to unsnap first
            bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());
            if (!unsnapped)
            {
                //NotifyUser("Cannot unsnap the sample.", NotifyType.StatusMessage);
            }
            return unsnapped;
        }
我得到的文件路径是这个
filePath=C:'Users'Prateek'Pictures'IMG_0137.JPG

我不知道哪里出错了

使用文件选择器在图像UI元素中设置图片

我不确定这是否会解决问题,这是我所做的设置我的图像源。

使用位图图像作为图像的源

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.";
        }
相关文章: