[WP8.1][C#]Why when I try to upload an Image with Windows Phone it uploads a random thing?

I'm trying to upload an image from my Windows Store App (Windows Phone 8.1) to hosting (using PHP) but always when I "uploaded" the image, and I check its result the "image" it's a corrupted file. I think it's related to the byte array
conversion but I haven't found another way to do it. This is my code:
C# Code:
byte[] ConvertBitmapToByteArray()
WriteableBitmap bmp = bitmap;
using (Stream stream = bmp.PixelBuffer.AsStream())
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
public async Task<string> Upload()
try
using (var client = new HttpClient())
using (var content =
new MultipartFormDataContent())
byte[] data = ConvertBitmapToByteArray();
content.Add(new StreamContent(new MemoryStream(data)), "userfile", fileNewImage);
using (
var message =
await client.PostAsync("http://xplace.com/uploadtest.php", content))
var input = await message.Content.ReadAsStringAsync();
return input;
catch (Exception ex)
return null;
PHP code:
<?php
header('Content-Type: application/json');
$uploaddir = getcwd();
$uploadfile = $uploaddir . "/" . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo '{"result": "sucessfull"}';
} else {
echo '{"result": "-1"}';
}?>
I wish someone could give me an idea about what I my mistake and how to fix it. Thanks in advance for your worthy knowledge.
Federico Navarrete

This is the answer to the problem someone gave me an idea that I have missed to translate to BMP.
byte[] ConvertBitmapToByteArray()
WriteableBitmap bmp = bitmap;
using (Stream stream = bmp.PixelBuffer.AsStream())
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
public async Task<string> Upload()
try
using (var client = new HttpClient())
using (var content =
new MultipartFormDataContent())
byte[] data = ConvertBitmapToByteArray();
using (var stream = new InMemoryRandomAccessStream())
// encoder *outputs* to stream
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
// encoder's input is the bitmap's pixel data
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight,
(uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, data);
await encoder.FlushAsync();
content.Add(new StreamContent(stream.AsStream()), "userfile", fileNewImage);
using (
var message =
await client.PostAsync("http://xplace.com/uploadtest.php", content))
var input = await message.Content.ReadAsStringAsync();
return input;
catch (Exception ex)
return null;
Now I have a different problem that after I uploaded the Image to Azure, I cannot execute the PHP code this one:
$uploaddir = getcwd();
$uploadfile = $uploaddir . "/" . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
chmod($uploadfile, 0755);
$Test = new Display($_FILES['userfile']['name']);
echo '{"result": "' . $Test->getNumber($_REQUEST['color'], false) . '"}';
//unlink($uploadfile);
} else {
echo '{"result": "-1"}';
I'd like to know what could be my bug because I don't understand why I can access from the URL, too to bit I cannot use it, maybe it's how I assigned the permissions but with or without the chmod, it
doesn't change at all. I have even tried other hostings and the problem is the same when I enter to the File Manager, there are only my PHP files and it doesn't allow me to manage the image.
Thanks for your worthy knowledge and experience.
Federico Navarrete

Similar Messages

Maybe you are looking for

  • Multi-factor Authentication?

    Multi-factor authentication will soon be mandatory for several of my applications. I need to know if CF has any built-in functionality, either stock or via custom tags, to handle any of the common multi-factor tools. How are other people handling thi

  • How to generate a pwm whose duty cycle has to be varied depending on the frequency of the input trigger??

    Sir/madam, I am quite new to the Labview FPGA module.  I am currently working on an application where i have to generate a pwm to control the ON time duration . The pwm ON time depends on the frequency of an input signal.I want to retrieve the ON tim

  • PS image to InDesign.

    How do I make the PS background transparent. I just want the radio to be visible in front of another graphic. TIA.

  • Lost audio control after update

    Hi - My audio progress bar shows full audio image on the desktop at all times and audio can't be muted or turned down. It makes this rhythmic metronome sound, flap, flap, flap. I have my speakers hooked up so I use them to mute the sound. Mute on G5

  • What browsers can I use for mac os x 10.5.8?

    Safari is updated but has not been working for the past few months and I have submitted it SO many times. I have it open for 30 seconds and it crashes EVERY time. I'm currently using a very old chrome version but I can watch any videos at all on it.