【问题标题】:Convert imageSource coming from Xam.Plugin.Media 5.0.1 to byte array in Xamarinforms?将来自 Xam.Plugin.Media 5.0.1 的 imageSource 转换为 Xamarin Forms 中的字节数组?
【发布时间】:2020-09-10 19:48:54
【问题描述】:
if (!CrossMedia.Current.IsPickPhotoSupported)
        {
            await Application.Current.MainPage.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 tmpSrc = ImageSource.FromStream(() =>
        {
            var stream = file.GetStream();
            file.Dispose();
            return stream;
        });

ImageSource toBeConverted = tmpSrc;

  • 我希望将变量 toBeConverted 转换为 Byte[] 所以 我可以将它发送到我的 webapi ...

【问题讨论】:

  • File.ReadAllBytes(file.Path);
  • 可以吗?

标签: c# arrays xamarin.forms imagesource


【解决方案1】:

ImageSource 是一种为 Xamarin.Forms.Image 提供源图像以显示某些内容的方法。如果您已经在屏幕上显示某些内容,那么您的 Image 视图中填充了来自其他地方的数据,例如文件或资源或存储在内存中的数组中...... .您可以保留对它的引用并根据需要上传,而不是尝试从 ImageSource 取回该数据。

所以你可以在选择照片后从文件中获取字节数组。

var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
    {
        PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
    });
if (file == null)
return;    
var bytes = File.ReadAllBytes(file.Path); // you could get  the byte[] here from the file path.

【讨论】:

    【解决方案2】:
    • 这段代码也对我有用...

       private async void Capture()
       {
           if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
           {
               await Application.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "OK");
               return;
           }
      
           var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
           {
               Directory = "Test",
               SaveToAlbum = true,
               CompressionQuality = 75,
               CustomPhotoSize = 50,
               PhotoSize = PhotoSize.Medium,
               DefaultCamera = CameraDevice.Front
           });
      
           if (file == null)
               return;
      
           var stream = file.GetStream();
           if (stream != null)
           {
               var StreamByte = ReadAllBytes(stream);
               var NewStream = new MemoryStream(StreamByte);
               // stream = mystream;
               Device.BeginInvokeOnMainThread(() => {
                   ImageSource = ImageSource.FromStream(() => NewStream);
               });
      
               student.ProfilePicture = StreamByte;
           }
       }
       public byte[] ReadAllBytes(Stream instream)
       {
           if (instream is MemoryStream)
               return ((MemoryStream)instream).ToArray();
      
           using (var memoryStream = new MemoryStream())
           {
               instream.CopyTo(memoryStream);
               return memoryStream.ToArray();
           }
       }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      相关资源
      最近更新 更多