将图像转换为 Base64String [英] Convert Image to Base64String
问题描述
我需要一些帮助来将图像转换为 base64string.我曾在 stackoverflow 中使用过类似问题的解决方案,但出现错误.
I need some help in convert image to base64string. I had used solution from similar question in stackoverflow but an error occured.
使用的解决方案:在 wp8 中将图像转换为 base64
问题出在我曾经设置为可写位图的图像源上,但结果是空异常.
The problem lies on the Image's Source I used to set as a writeablebitmap but the outcome was a null exception.
这是我的图片来源:
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
我现有的代码作为参考:
My existing code as reference :
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;
我真的很想寻求帮助,因为这是我的主要项目.提前致谢!
I'm really desperate for help as this is my major project. Thanks in advance!
推荐答案
代码工作正常,只是它需要一个 EventHandler.我使用了 Button_Click 侦听器 来运行代码,它运行良好!
The codes are working correctly, just that it needs an EventHandler. I had used a Button_Click Listener to run the codes and it worked perfectly!
这篇关于将图像转换为 Base64String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!