2013-07-04 188 views
1

我需要一些帮助将图像转换为base64string。我曾使用过类似问题的解决方案,但出现了一个错误。使用将图像转换为Base64String

解决方案: convert image into base64 in wp8

的问题在于对图片的来源我曾经设置为WriteableBitmap的,但结果是空例外。

这是我的图像源:

image123.Source = new BitmapImage(new Uri("/correct1.jpg", UriKind.Relative)); 

错误在这条线发生:

WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)image123.Source); 

的错误:

An exception of type 'System.NullReferenceException' occurred in System.Windows.ni.dll but was not handled in user code 

我的现有代码作为参考:

 image123.Source = new BitmapImage(new Uri("/correct1.jpg", UriKind.Relative)); 

     byte[] bytearray = null; 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      if (image123.Source != null) 
      { 
       WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)image123.Source); 
       wbitmp.SaveJpeg(ms, 46, 38, 0, 100); 
       bytearray = ms.ToArray(); 
      } 
     } 
     str = Convert.ToBase64String(bytearray); 

     binarytxt.Text = str; 

我真的非常渴望帮忙,因为这是我的主要项目。提前致谢!

+0

我复制粘贴你的代码,它工作正常,你确定'correct1.jpg'正确加载到图像 –

+0

@ sa_ddam213是我注释了我的代码期望的图像源,图像加载到控制。在对Uri字符串进行多处更改后,我仍然遇到同样的错误。顺便说一下,我正在使用Windows Phone 8应用程序模板,只是为了澄清。 –

回答

0

代码工作正常,只是需要一个EventHandler。我使用了一个Button_Click监听器来运行代码,它的工作完美!