将画廊中的图像转换为缩略图(Xamarin Forms)

4

这是我在我的pcl项目(ios/android)中从图库选择图片的代码:

        protected async Task PickImage()
    {
        try
        {
            Stream stream = await DependencyService.Get<IPicturePicker>().GetImageStreamAsync();

            {
                Image image = new Image
                {
                    Source = ImageSource.FromStream(() => stream),
                    BackgroundColor = Color.Gray
                };

                byte[] ImageData = Utils.Base64Utils.ToByteArray(stream);
                _base64String = Convert.ToBase64String(ImageData);

                editar_foto_perfil.Source = ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(_base64String)));

                user.trocaImage = _base64String;

                if (Device.OS == TargetPlatform.iOS)
                {
                    user.cont_datanascimento = editar_date_datanasc.Date.ToString("yyyyMMdd");

                    if (editar_entry_nome.Text != null)
                        user.cont_nome = editar_entry_nome.Text;

                    if (editar_picker_estado.SelectedIndex != -1)
                        user.cont_estado = editar_picker_estado.Items[editar_picker_estado.SelectedIndex].ToString();

                    if (editar_picker_cidade.SelectedIndex != -1)
                        user.cont_cidade = editar_picker_cidade.Items[editar_picker_cidade.SelectedIndex].ToString();

                    if (editar_entry_senha.Text != null)
                        user.usua_senha = editar_entry_senha.Text;

                    if (editar_entry_email.Text != null)
                        user.usua_login = editar_entry_email.Text;

                    menu.RecriaEditarIOS(user);
                }
            }
        }
        catch (Exception ex)
        {
            var s = ex.Message;
        }
    }

有时候我无法将选取的图片发送到服务器。通常情况下,这种情况发生在图片过大时,因此我想将其调整为小图像并发送到服务器。


更新

如我们的朋友在评论中建议的那样,我正在尝试使用Crossmedia插件……然后我改变了我的方法:

protected async Task PickImage()
    {
        try
        {
            //Stream stream = await DependencyService.Get<IPicturePicker>().GetImageStreamAsync();
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
                return;
            }
            else
            {
            var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
            {
                PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
            });             
        }
      }
        catch (Exception ex)
        {
            var s = ex.Message;
        }
    }

但是,file 始终为null。

您可以参考以下链接:https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/ - York Shen
但是,如果图像不是位图呢? - Joyce de Lanna
因为它们是用户图库的图片 - Joyce de Lanna
你解决了你的问题吗? - York Shen
我还没有解决这个问题。 - Joyce de Lanna
1个回答

0
从我的PCL项目的图库中选择一张图片,并将其调整大小为小图像。
您可以使用MediaPlugin来实现此功能,以下是它的简单用法
pickPhoto.Clicked += async (sender, args) =>
{
    if (!CrossMedia.Current.IsPickPhotoSupported)
    {
      DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
      return;
    }
     var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
                  {
                      PhotoSize =  Plugin.Media.Abstractions.PhotoSize.Medium,
     });


    if (file == null)
      return;

    var stream = file.GetStream();
}

PickMediaOptions用于调整所选图像的大小,您可以在此处找到源代码。

更新:

这是我的代码,在我的端上运行良好:

private async void button_Clicked(object sender, EventArgs e)
{
    await PickImage();
}

protected async Task PickImage()
{
    try
    {
        await CrossMedia.Current.Initialize();

        if (!CrossMedia.Current.IsPickPhotoSupported)
        {
            DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
            return;
        }
        else
        {
            var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
            {
                PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
            });

            if (file == null)
                return;

            image.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                file.Dispose();
                return stream;
            });
        }
    }
    catch (Exception ex)
    {
        var s = ex.Message;
    }
}

文件始终为空。 - Joyce de Lanna
请您能否发布您的代码并提供详细的步骤来重现您的问题? - York Shen
谢谢您的回复,我会查看一下。 - York Shen
@JoycedeLanna,我测试了你的代码,在我的电脑上运行良好。你能否分享一个基本演示,通过在线存储库重现此问题?这样我们就可以确保我们正在验证你使用的确切内容。 - York Shen
我会尝试创建一个。 - Joyce de Lanna
让我们在聊天中继续这个讨论 - York Shen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接