I'm making a project on Windows Phone where user can take a photo, save it on phone and next upload on my server. So there are two projects, WP8.1 and ASP.NET WEB API.
Currently I don't know how to upload photo to my server(from phone), I even don't know how to catch it in API. What's the best way to make it?
Here is my method to show on the screen(phone), picture which was taken by user.
private async void LoadCapturedphoto(string filename)
{
//load saved image
StorageFolder pictureLibrary = KnownFolders.SavedPictures;
StorageFile savedPicture = await pictureLibrary.GetFileAsync(filename);
ImageProperties imgProp = await savedPicture.Properties.GetImagePropertiesAsync();
var savedPictureStream = await savedPicture.OpenAsync(FileAccessMode.Read);
//set image properties and show the taken photo
bitmap = new WriteableBitmap((int)imgProp.Width, (int)imgProp.Height);
await bitmap.SetSourceAsync(savedPictureStream);
takenImage.Source = bitmap;
takenImage.Visibility = Visibility.Visible;
}
I think that I should convert WriteableBitmap to Byte[], send these Byte[] by API to server and on server convert Byte[] to JPG/PNG/etc.
private byte[] ConvertBitmapToByteArray(WriteableBitmap bitmap)
{
WriteableBitmap bmp = bitmap;
using (Stream stream = bmp.PixelBuffer.AsStream())
{
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
Any ideas? Do you think it's good idea to make it like that? WriteableBitmap -> Byte[] and next on server Byte[] -> JPG/PNG etc. Is it even possible?
If it can be done much more easier please write some samples.
it's my method for calling api methods
public string apiCommand(string api, string json)
{
using (var httpClient = new HttpClient())
{
HttpContent content = new StringContent(json, Encoding.UTF8);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = httpClient.PostAsync(Variables.apiURL + api, content).Result;
response.EnsureSuccessStatusCode();
Task<string> responseBody = response.Content.ReadAsStringAsync();
//var msg = new MessageDialog(responseBody.Result.ToString());
if (response.StatusCode.ToString() != "OK")
{
return "ERROR: " + response.StatusCode.ToString();
}
else
{
return "SUCCES: " + responseBody.Result.ToString();
}
}
}