[英]how to get byte[] for Image on Xamarin.forms
I am using Xamarin.Forms , So when I am trying to choose photo from Gallery(I used https://components.xamarin.com/view/mediaplugin to handle with photos ), I need to get byte[]
for image to use it on web service, how I can do that. 我正在使用Xamarin.Forms ,所以当我尝试从Gallery中选择照片时(我使用https://components.xamarin.com/view/mediaplugin处理照片),我需要获取
byte[]
以便使用图像它在网络服务上,我该怎么做。
Check My Code on the Following: 检查以下内容的我的代码:
ChoosePhotoButton.Clicked += async (sender, args) =>
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
return;
}
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
OptionalInfoPage.DriverImage = image;
};
I think you are doing well if your code is returning a valid stream! 如果您的代码返回有效的流,我认为您做得不错! just try adding this instead of returning the stream ,,
只需尝试添加它而不是返回流,
byte[] ImageBytes = stream.ToArray();
return ImageBytes;
I get it from file directly by use file.GetStream().CopyTo(memoryStream)
check the following code : 我通过使用
file.GetStream().CopyTo(memoryStream)
直接从文件中获取它,检查以下代码:
using (var memoryStream = new System.IO.MemoryStream())
{
file.GetStream().CopyTo(memoryStream);
// file.Dispose();
byte[] ImageBytes = memoryStream.ToArray();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.