我们如何从 imageToUpload.Source 接收图像文件并传递给另一个方法

How can we received a image file from imageToUpload.Source and passed into another method

如何接收显示在 <Image x:Name="imageToUpload" WidthRequest="40" HeightRequest="40"/> 下的 'photo' 并传递给 Xamarin Forms 中的变量?我在这一行得到 file/image var file = await CrossMedia.Current.TakePhotoAsync(.... 需要传递到 RegisterSave_OnClicked() 方法并进一步保存到 SQLite 数据库

 var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    Directory = "Pictures",
                    Name = "test.jpg",
                    PhotoSize = PhotoSize.Small,
                    CompressionQuality = 75,
                    CustomPhotoSize = 5,
                    DefaultCamera = CameraDevice.Front,
                });

//注册按钮保存代码如下:

public async void RegisterSave_OnClicked(object sender, EventArgs e)
        {
            int count = (from y in conn.Table<PlayerDetails>().Where(y => y.Email == playerEmail) select y).Count();
            if(count!=0)
            {
                var updatePlayer = (from y in conn.Table<PlayerDetails>().Where(y => y.Email == playerEmail) select y);
                foreach (var update_Player in updatePlayer)
                {
                    update_Player.FullName = fullNameEntry.Text;
                    update_Player.Mobile = mobileEntry.Text;
                    // code continues here .......
                    // assuming conn is an SQLiteConnection
                    conn.Update(update_Player);   
                }               
                await Navigation.PushAsync(new MainPage());
            }
            else
            {
                PlayerDetails playerDetails = new PlayerDetails();
                playerDetails.FullName = fullNameEntry.Text;
                playerDetails.Mobile = mobileEntry.Text;
                // code continues here .......

如果你想将图片源保存到数据库,我们可以将它转换成byte[]数组。

public  byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16*1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

MediaFileGetStream().

您可以将其用于 Stream,然后将其转换为 byte[]。这是一种方法:

1.define contentPage 中的流

Stream imageStream;

拍照后初始化。

imageStream = file.GetStream();

点击按钮时调用

var imageArr= ReadFully(imageStream );