如何在我的byte []中添加我的图像? Xamarin表格,C#

时间:2016-06-21 19:58:18

标签: c# xamarin xamarin.forms

我有一个带有字节的viewmodel,如下所示:

public byte[] ReadStream(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();
        }

    }

我通过这样做在我的内容网页上找到了它。

    CameraViewModel cameraOps = null;

    public PhotoPage ()
    {
        InitializeComponent ();
    }

    cameraOps = new CameraViewModel ();

我的点击功能,我尝试将字节发送到我的数据库:

async void createImage (object s, EventArgs a)
    {

    var createImagetheimg = await phpApi.createPhotoTwo (cameraOps.ReadStream (imgPicked.Source));

    //imgPicked is my image that I want to send.
    }

它不允许我在字节内添加要发送到数据库的图像。我收到错误:"无法将Xamarin.Forms.ImageSource表达式转换为System.IO.Stream" 我是否需要调整" ReadStream"输入

这是我尝试将图像发送到的地方:

static public async Task <bool>  createPhotoTwo (byte [] imgData)
{   
    var httpClientRequest = new HttpClient ();

    var postData = new Dictionary <string, object> ();

    postData.Add ("photoData", imgData);

    var jsonRequest = JsonConvert.SerializeObject(postData);

    HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");

    var result = await httpClientRequest.PostAsync("http://www.myadress.com/put.php", content);
    var resultString = await result.Content.ReadAsStringAsync ();

    return  true;

}

更新代码:

private async void btnPickPicture_Clicked (object sender, EventArgs e)
    {
        await cameraOps.SelectPicture ();
        var file = await cameraOps.SelectPicture();
        imgPicked.Source = ImageSource.FromStream(() => file.Source);

        System.Diagnostics.Debug.WriteLine (imgPicked.Source);
        //StreamImageSource
        var createImagetheimg = await phpApi.createPhotoTwo (cameraOps.ReadStream (imgPicked.Source));
    }

0 个答案:

没有答案