4

我有一個Windows 8應用程序工作得很好,現在我想爲Windows Phone 8編寫相同的應用程序,但我只是得到一個黑色的圖像,而不是正確的形象。POST圖像到Windows Phone 8的網絡服務器

這是我上傳的圖像文件

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

代碼,這是對我的圖像轉換爲字節數組的代碼

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

大家有一個想法,爲什麼我只得到一個黑色的圖像,而不是正確的圖像?該圖像由PhotoChooseTask選擇。

+0

你能告訴我們這個PHP代碼嗎? – IMX 2013-12-28 11:53:25

回答

3

的PhotoChooseTask已經給你一個流,那麼你只需要使用代替(不能使用位還因爲它仍是忙着寫的設備和生成縮略圖等)

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

感謝您的回答,但它不起作用。現在在服務器上生成一個文件,但它的大小爲0,因此沒有圖像。你能告訴我爲什麼嗎? – 2013-03-16 05:14:50

+0

@ThomasSebastianJensen流的位置需要設置爲0 – 2013-07-12 22:44:31