Skip to main content
 首页 » 编程设计

c#之如何在 Xamarin.forms 上获取图像的 byte[]

2024年02月05日14lori

我正在使用 Xamarin.Forms,所以当我尝试从图库中选择照片时(我使用 https://components.xamarin.com/view/mediaplugin 来处理照片),我需要获取 byte[] 用于在 Web 服务上使用图像,我该怎么做。

在以下方面检查我的代码:

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;