C# e.ChosenPhoto中的Base64显然已损坏
我使用这段代码使用WebClient将图像上传到IMGURAPIv3。选择的图像是Windows Phone 7.1 emulator提供的7张照片之一,或者是模拟的相机图像。当我尝试加载图像时,它们基本上是灰色的。我是否正确生成base64和/或在创建字节[]和base64之前是否需要先渲染图片的位图C# e.ChosenPhoto中的Base64显然已损坏,c#,windows-phone-7,imgur,C#,Windows Phone 7,Imgur,我使用这段代码使用WebClient将图像上传到IMGURAPIv3。选择的图像是Windows Phone 7.1 emulator提供的7张照片之一,或者是模拟的相机图像。当我尝试加载图像时,它们基本上是灰色的。我是否正确生成base64和/或在创建字节[]和base64之前是否需要先渲染图片的位图 提前谢谢 使用类似于Uri.EscapeDataString的内容来转义数据,以便不解释特殊的URL字符。我使用这种方法 //convert photo to baos
提前谢谢 使用类似于
Uri.EscapeDataString的内容来转义数据,以便不解释特殊的URL字符。我使用这种方法
//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);
并将字节[]
上传到服务器。希望对您有所帮助尝试使用类似于Uri.EscapeDataString的内容转义base64字符串
谢谢@PeterRitchie--这很有效。我使用了EscapeDataString方法解决了这个问题,我使用的是Android版本的基本逻辑,它不需要转义base64数据(baos->base64->post请求)
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;
}
}