Windows phone 7 如何在Windows Phone中将图像转换为base64字符串?
我正在开发一个windows phone应用程序,其中我必须将图像转换为base64字符串,并且必须通过Web服务传递该字符串。所以我尝试了很多方法,但我无法在每次收到目标调用错误时发送它。使用此代码,我可以从库中选择图像,但我无法通过web服务发送 我使用以下代码来隐藏图像:Windows phone 7 如何在Windows Phone中将图像转换为base64字符串?,windows-phone-7,c#-4.0,windows-phone-8,json.net,Windows Phone 7,C# 4.0,Windows Phone 8,Json.net,我正在开发一个windows phone应用程序,其中我必须将图像转换为base64字符串,并且必须通过Web服务传递该字符串。所以我尝试了很多方法,但我无法在每次收到目标调用错误时发送它。使用此代码,我可以从库中选择图像,但我无法通过web服务发送 我使用以下代码来隐藏图像: private void photoChooserTask_Completed(object sender, PhotoResult e) { BitmapImage image = new BitmapImag
private void photoChooserTask_Completed(object sender, PhotoResult e)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
this.imageTribute.Source = image;
byte[] bytearray = null;
using (MemoryStream ms = new MemoryStream())
{
if (imageTribute.Source == null)
{
}
else
{
WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)imageTribute.Source);
wbitmp.SaveJpeg(ms, 40, 40, 0, 82);
bytearray = ms.ToArray();
}
}
strimage = Convert.ToBase64String(bytearray);
}
所以如果有人知道这件事,请帮帮我。提前准备好
编辑
编辑
在这里,我上传了错误的堆栈跟踪。因此,请检查并回复我。目标调用异常错误告诉您,应用程序在调用一个方法时崩溃,这可能是很多事情。真正的错误在InnerException中
查看TargetInvocationException对象的InnerException属性。这将向您显示堆栈跟踪和实际抛出的错误。从资源或isolatedStorage将文件放入流中
//getting file from resource
var resource = Application.GetResourceStream(new Uri("image.jpg", UriKind.Relative));
//get Stream Data
StreamReader streamReader = new StreamReader(resource.Stream);
//initializing bytearray to stream length
byte[] imageData = new byte[streamReader.Length];
//wriing from stream to imagdata
streamReader.Read(imageData, 0, imageData.Length);
streamReader.Close();
使用isolatedStorageFile从隔离存储中读取
现在,您的图像数据位于imageData中,要将其转换为base64,请使用:
var baseString = Convert.ToBase64String(imageData);
我已经编辑了我的代码,请查看代码并告诉我为什么会出现目标调用异常。我正在使用Webclient将图像发送到web服务。请发布堆栈跟踪和内部。事实上,我对开发非常陌生。请告诉我如何查看堆栈跟踪和InnerException。我现在可以看到的一件事是e.Result引发了一个类型为“System.Reflection.TargetInvocationException”的异常错误。但我想我无法获取所选图像的名称。那个么我怎样才能给出一个特定的名字呢?要上传一张照片,你们应该使用多部分/表单数据请求。要做到这一点,请使用HttpWebRequest。实际上,web客户端在WindowsPhone8.Thanx中没有UploadFileAsync或DownloadFileAsync方法。但是你能给我发一个关于如何使用HttpWebRequest的示例给我的特定代码吗?请跟随。我也用同样的方法获得了成功。它是用于音频文件的。你能告诉我一些照片吗。有时我会从图书馆中选择照片,有时我会用相机拍摄照片并将其作为照片上传。
var baseString = Convert.ToBase64String(imageData);