0

I have a viewmodel with a byte that looks like this:

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();
        }

    }

And I reach it on my contentpage by doing this.

    CameraViewModel cameraOps = null;

    public PhotoPage ()
    {
        InitializeComponent ();
    }

    cameraOps = new CameraViewModel ();

And my clickfunction where I try to send the Byte to my DB:

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.
    }

It does not let me add the image I want to send to my database inside the byte. I get the error: "Cannot convert Xamarin.Forms.ImageSource expresssion to type System.IO.Stream" Do I need to adjust the "ReadStream" input?

THis is where I try to send the image into:

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;

}

UPDATED CODE:

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));
    }
medvedo
  • 543
  • 2
  • 8
  • 23
  • What is the exception which is thrown? – wake-0 Jun 21 '16 at 19:59
  • "Cannot convert Xamarin.Forms.ImageSource expresssion to type System.IO.Stream" – medvedo Jun 21 '16 at 20:01
  • Have you searched for the exception and checked the entries: (http://stackoverflow.com/questions/27532462/is-there-a-cross-platform-solution-to-imagesource-to-byte) – wake-0 Jun 21 '16 at 20:04
  • you need two different things - a ImageSource and a byte[]; both of these can be created from a stream, but they are not interchangeable with one another. – Jason Jun 21 '16 at 20:10
  • Been googling a bit. Very unsure about how I should proceed. Should I do something with this: (Stream input, Image image) ? Like add another value in there? – medvedo Jun 21 '16 at 20:40
  • No. You have stream returned by your camera code. You know how to create a StreamImageSource using a stream. And you have the code you posted above to create a byte[] from a stream. – Jason Jun 21 '16 at 20:49
  • So inside the `var createImagetheimg = await phpApi.createPhotoTwo (cameraOps.ReadStream (*ADD IT HERE*));` I add the image that is an 'StreamImageSource'? – medvedo Jun 21 '16 at 21:18
  • I would just create a byte[] at the same time you create your ImageSource, and then when you're ready to upload pass it that byte[]. – Jason Jun 21 '16 at 21:33
  • Check out my updated code. When I run the image in the log now it is an StreamImageSource but it does not match with the System.IO.Stream – medvedo Jun 22 '16 at 13:47

0 Answers0