如何在Windows Phone中将图像转换为base64字符串?

时间:2013-08-08 08:25:05

标签: windows-phone-7 c#-4.0 windows-phone-8 json.net

我正在开发一个Windows Phone应用程序,我必须将image转换为base64字符串,我必须通过Web Service传递该字符串。所以我尝试了很多方法,但每次我收到错误时都无法发送它作为“目标调用错误”。使用此代码,我可以从库中选择图像,但我无法通过Web服务发送。

我使用以下代码来隐藏图像:

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

所以,如果有人知道,请帮助我。提前完成。

修改

void uploadphoto()
{
WebClient webClient1 = new WebClient();
    webClient1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient1_DownloadStringCompleted);
webClient1.DownloadStringAsync(new Uri("Web Service"));
}
void webClient1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var rootobject1 = JsonConvert.DeserializeObject<RootObject1>(e.Result);
int error = rootobject1.response.errorFlag;
    string message = rootobject1.response.msg;
if (error == 0)
        {
                MessageBox.Show(message);
        }
    else
        {
                MessageBox.Show(message);
        }
}
public class Response1
{
public int errorFlag { get; set; }
    public string msg { get; set; }
    public List<string> uploadedImageNames { get; set; }
}
public class RootObject1
{
public Response1 response { get; set; }
}
private void ImageUpload(object sender, RoutedEventArgs e)
{
    //MessageBoxResult mb = MessageBox.Show("Select the mode of uploading the picture", "", MessageBoxButton.OKCancel);
    Popup popup = new Popup();
    photoSelection photo = new photoSelection();
    popup.Child = photo;
    popup.IsOpen = true;
    photo.camera.Click += (s, args) =>
        {
            photoCameraCapture.Show();
            popup.IsOpen = false;
        };
    photo.library.Click += (s, args) =>
        {
            photoChooserTask.Show();
            popup.IsOpen = false;
        };
}

修改

StackTrace

这里我上传了错误的堆栈跟踪。所以请检查并回复我。

2 个答案:

答案 0 :(得分:0)

目标调用异常错误告诉您应用程序在调用可能很多事情的方法时崩溃了。真正的错误在于InnerException。

查看TargetInvocationException对象的InnerException属性。这将显示堆栈跟踪和引发的实际错误。

答案 1 :(得分:-1)

从资源或来自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);