[英]how to get byte[] for Image on Xamarin.forms
我正在使用Xamarin.Forms ,所以當我嘗試從Gallery中選擇照片時(我使用https://components.xamarin.com/view/mediaplugin處理照片),我需要獲取byte[]
以便使用圖像它在網絡服務上,我該怎么做。
檢查以下內容的我的代碼:
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;
};
如果您的代碼返回有效的流,我認為您做得不錯! 只需嘗試添加它而不是返回流,
byte[] ImageBytes = stream.ToArray();
return ImageBytes;
我通過使用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.