【发布时间】:2013-07-01 22:59:02
【问题描述】:
我需要一些帮助来将图像转换为 base64string。我在stackoverflow中使用了类似问题的解决方案,但发生了错误。
使用的解决方案: convert image into base64 in wp8
问题出在我用来设置为可写位图的 Image 的 Source 上,但结果是 null 异常。
这是我的图片来源:
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;
我真的非常渴望帮助,因为这是我的主要项目。提前致谢!
【问题讨论】:
-
我复制粘贴了您的代码,它工作正常,您确定
correct1.jpg已正确加载到图像中 -
@sa_ddam213 是的,我注释掉了我对图像源的期望代码,图像已加载到控件上。在对 Uri 字符串进行多次更改后,我仍然遇到相同的错误。顺便说一句,我使用的是 windows phone 8 应用程序模板,只是为了澄清。
标签: c# image windows-phone-8 base64 converter