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