0

如何接收显示<Image x:Name="imageToUpload" WidthRequest="40" HeightRequest="40"/>在 Xamarin Forms 下的“照片”并将其传递给变量?我在这一行获取文件/图像 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 .......

在此处输入图像描述

4

1 回答 1

1

如果要将 imageSource 保存到数据库,我们可以将其转换为 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();
    }
}

MediaFile拥有GetStream().

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

1.在你的 contentPage 中定义一个流

Stream imageStream;

拍照后初始化。

imageStream = file.GetStream();

并在您单击按钮时调用它

var imageArr= ReadFully(imageStream );
于 2019-08-09T06:58:36.630 回答