2013-04-21 41 views
2
  //convert photo to baos 
      var memoryStream = new System.IO.MemoryStream(); 
      e.ChosenPhoto.CopyTo(memoryStream); 
      //string baos = memoryStream.ToString(); 
      byte[] result = memoryStream.ToArray(); 
      String base64 = System.Convert.ToBase64String(result); 
      String post_data = "&image=" + base64; 
      ... 
      wc.UploadStringAsync(imgur_api,"POST",post_data); 

我使用此代碼上傳到使用Web客戶端的Imgur API V3的圖像。所選圖像是Windows Phone 7.1仿真器提供的7張照片之一,或者是模擬的照相機圖像。當我嘗試加載圖像時,它們大致呈灰色混亂。我是否正確地生成了base64和/或在創建byte []和base64之前是否需要先渲染圖片的位圖?的Base64明顯損壞

在此先感謝!

+0

嘗試使用'Uri.EscapeDataString'類似的東西來轉義base64字符串 – 2013-04-21 15:51:27

+1

謝謝@PeterRitchie - 工作。我使用了https://github.com/DeadlyBrad42/Poof/blob/master/ImgurUpload.cs中的EscapeDataString方法。 – digitalsteez 2013-04-21 16:08:53

回答

3

使用類似Uri.EscapeDataString的東西來轉義數據,以便不解釋特殊的URL字符。

+0

解決了這個問題,我使用的是Android版本中的基本邏輯,它不需要轉義base64數據( baos - > base64 - >發佈請求) – digitalsteez 2013-04-21 20:33:18

0

我用這個

private void PhotoChooserTaskCompleted(object sender, PhotoResult e) 
    { 
     if (e.TaskResult != TaskResult.OK) return; 
     var bimg = new BitmapImage(); 
     bimg.SetSource(e.ChosenPhoto); 
     var sbytedata = ReadToEnd(e.ChosenPhoto); 
    } 

public static byte[] ReadToEnd(System.IO.Stream stream) 
    { 
     long originalPosition = stream.Position; 
     stream.Position = 0; 

     try 
     { 
      byte[] readBuffer = new byte[4096]; 

      int totalBytesRead = 0; 
      int bytesRead; 

      while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) 
      { 
       totalBytesRead += bytesRead; 

       if (totalBytesRead == readBuffer.Length) 
       { 
        int nextByte = stream.ReadByte(); 
        if (nextByte != -1) 
        { 
         byte[] temp = new byte[readBuffer.Length * 2]; 
         Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length); 
         Buffer.SetByte(temp, totalBytesRead, (byte)nextByte); 
         readBuffer = temp; 
         totalBytesRead++; 
        } 
       } 
      } 

      byte[] buffer = readBuffer; 
      if (readBuffer.Length != totalBytesRead) 
      { 
       buffer = new byte[totalBytesRead]; 
       Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); 
      } 
      return buffer; 
     } 
     finally 
     { 
      stream.Position = originalPosition; 
     } 
    } 

並上傳byte[]到服務器。希望它的幫助