以缩略图的形式从图库中转换图像(xamarin 形式)
convert image from gallery in thumbnail (xamarin forms)
这是我在 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 to implement this function, here is its 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();
}
Class PickMediaOptions is used to resize the selected image, you could find the source code 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;
}
}
这是我在 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 to implement this function, here is its 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();
}
Class PickMediaOptions is used to resize the selected image, you could find the source code 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;
}
}