POST image to web server in windows phone 8

I'm having a Windows 8 app which is working pretty well and now I want to write the same app for Windows Phone 8, but I'm only getting a black image and not the right image.

This is my code for uploading the image file

if ((_fileType == ".jpg" || _fileType == ".png" || _fileType == ".jpeg") && _fileSize < 3500000)
{
    byte[] myPicArray = ConvertToBytes(_bmpFile);
    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri(MYURI);
    MultipartFormDataContent form = new MultipartFormDataContent();
    HttpContent content = new ByteArrayContent(myPicArray);
    form.Add(content, "media", _randomStringFileName + _fileType);

    HttpResponseMessage response = await httpClient.PostAsync("upload.php", form);
}

and this is the code for converting my image to a byte array

private byte[] ConvertToBytes(BitmapImage bitmapImage)
{
    using (MemoryStream ms = new MemoryStream())
    {
        WriteableBitmap btmMap = new WriteableBitmap
            (bitmapImage.PixelWidth, bitmapImage.PixelHeight);

        // write an image into the stream
        Extensions.SaveJpeg(btmMap, ms,
            bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);

        return ms.ToArray();
    }
}

Has anybody an idea why I'm only getting a black image and not the right image? The image was selected by the PhotoChooseTask.

Upvotes: 4

Views: 2590

Answers (1)

Patrick F
Patrick F

Reputation: 1201

The PhotoChooseTask already gives you the Stream, so you'll just need to use that instead (You can't use the BitMap yet because it's still busy writing it to the device and generating thumbnails, etc)

        PhotoResult photoResult = e as PhotoResult;
        MemoryStream memoryStream = new MemoryStream();
        photoResult.ChosenPhoto.CopyTo(memoryStream);
        byte[] myPicArray = memoryStream.ToArray();

Upvotes: 3

Related Questions