First the pick the image from the galley using File Picker.
FileOpenPicker imagePicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".png", ".jpg" }
};
pickedImage = await imagePicker.PickSingleFileAsync();
if (pickedImage != null)
{
IRandomAccessStream displayStream = await pickedImage.OpenAsync(FileAccessMode.Read);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(displayStream);
imageBytes = await byteImgConvert.GetBytesFromFile(pickedImage);
strImagename = pickedImage.Name;
addpicture.Source = bitmapImage;
popupAddPicture.IsOpen = true;
txtImg.Text = txtcontent;
}
string serverUrl = "http://10.10.10.1/sampleapplication/imageupload/";
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
String webResponse="";
byte[] fileData = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(imageBytes.ToArray()).Replace("+", "%2B"));
byte[] prefix = System.Text.Encoding.UTF8.GetBytes("up_img=");
byte[] combinedData = new byte[fileData.Length + prefix.Length];
System.Buffer.BlockCopy(prefix, 0, combinedData, 0, prefix.Length);
System.Buffer.BlockCopy(fileData, 0, combinedData, prefix.Length, fileData.Length);
Stream requestStream = await webRequest.GetRequestStreamAsync();
requestStream.Write(combinedData, 0, combinedData.Length);
WebResponse response = await webRequest.GetResponseAsync();
StreamReader requestReader = new StreamReader(response.GetResponseStream());
webResponse = requestReader.ReadToEnd();
if (webResponse.Contains("success") == true)
{
MessageDialog uploadSuccess = new MessageDialog("Picture posted successfully");
await uploadSuccess.ShowAsync();
}
FileOpenPicker imagePicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".png", ".jpg" }
};
pickedImage = await imagePicker.PickSingleFileAsync();
if (pickedImage != null)
{
IRandomAccessStream displayStream = await pickedImage.OpenAsync(FileAccessMode.Read);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(displayStream);
imageBytes = await byteImgConvert.GetBytesFromFile(pickedImage);
strImagename = pickedImage.Name;
addpicture.Source = bitmapImage;
popupAddPicture.IsOpen = true;
txtImg.Text = txtcontent;
}
string serverUrl = "http://10.10.10.1/sampleapplication/imageupload/";
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
String webResponse="";
byte[] fileData = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(imageBytes.ToArray()).Replace("+", "%2B"));
byte[] prefix = System.Text.Encoding.UTF8.GetBytes("up_img=");
byte[] combinedData = new byte[fileData.Length + prefix.Length];
System.Buffer.BlockCopy(prefix, 0, combinedData, 0, prefix.Length);
System.Buffer.BlockCopy(fileData, 0, combinedData, prefix.Length, fileData.Length);
Stream requestStream = await webRequest.GetRequestStreamAsync();
requestStream.Write(combinedData, 0, combinedData.Length);
WebResponse response = await webRequest.GetResponseAsync();
StreamReader requestReader = new StreamReader(response.GetResponseStream());
webResponse = requestReader.ReadToEnd();
if (webResponse.Contains("success") == true)
{
MessageDialog uploadSuccess = new MessageDialog("Picture posted successfully");
await uploadSuccess.ShowAsync();
}