2013-04-21 45 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[]到服务器。希望它的帮助