Convert Image into byte array in Xamarin.Forms

后端 未结 7 952
春和景丽
春和景丽 2020-12-20 21:47

I need to pass an image (Image _thresholdedImage) like byte array... I don\'t know how I can do this. Any idea? Thank you!

_thresholdedImage.Sou         


        
7条回答
  •  礼貌的吻别
    2020-12-20 22:06

    It turns out that you can convert the MediaFile object to a byte array. So no need to convert the file to ImageSource or Image.

    var photo = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });
    
    byte[] imageArray = null;
    
    using (MemoryStream memory = new MemoryStream()) {
    
        Stream stream = photo.GetStream();
        stream.CopyTo(memory);
        imageArray = memory.ToArray();
    }
    

    Source: https://forums.xamarin.com/discussion/156236/how-to-get-the-bytes-from-the-imagesource-in-xamarin-forms
    Shoutout to user "ajaxer" in the Xamarin forums whose solution saved me after spending 3 days on this issue.

提交回复
热议问题