android– 从缩略图中的图库转换图像(xamarin表单)

android– 从缩略图中的图库转换图像(xamarin表单),第1张

概述这是我的代码,用于从我的pcl项目中的库中选择一个图像(ios/android)protectedasyncTaskPickImage(){try{Streamstream=awaitDependencyService.Get<IPicturePicker>().GetImageStreamAsync();{

这是我的代码,用于从我的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;        }    }

但是,文件始终为空

解决方法:

pick an image from the gallery in my PCL project and I want to resize it to a small image

您可以使用MediaPlugin来实现此功能,这是它的simple usage:

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用于调整所选图像的大小,您可以找到源代码here.

更新:

这是我的代码,它在我身边很好用:

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;    }}

总结

以上是内存溢出为你收集整理的android – 从缩略图中的图库转换图像(xamarin表单)全部内容,希望文章能够帮你解决android – 从缩略图中的图库转换图像(xamarin表单)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116720.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存