[英]Convert ImageSource to Base64String - WP8
我正在研究WP8应用程序,我在位置Resources \\ Graphics \\我想要显示来自这些文件夹的图像,但它没有选择路径。
这是我的代码:
<img src=\\"/Resources;component/Graphics/"+ImageName).Append("\\" ") this is in my string which i am using in my WebBrowserControl.
WebBrowserControl.NavigateToString(html); // here html is a string which has all the html code in it.
但它不显示图像。
所以我想将ImageSource - Resources;component/Graphics/"+ImageName
为Base64String怎么做?
我已经研究了很多例子,但它们都不兼容WP8。
它非常简单 - 将图像加载到字节数组中并调用
System.Convert.ToBase64String(imageArray)
。
话虽如此,这不会导致显示图像。 NavigateToString需要html。 见文档
您可以使用以下方法获取StreamInfo
:
Application.GetResourceStream(new Uri("Resources;component/Graphics/"+ImageName", System.UriKind.Relative));
然后,您可以将此流读取为字节数组。 之后,使用Convert.ToBase64String()
来获得你想要的。 尝试这个。 也许您可以阅读MSDN文档以了解如何使用Stream。
var img = Application.GetResourceStream(new Uri("Resources;component/Graphics/"+ImageName", System.UriKind.Relative));
var buffer = new byte[img.Stream.Length];
img.Stream.Seek(0, SeekOrigin.Begin);
img.Stream.Read(buffer, 0, buffer.Length);
var base64 = Convert.ToBase64String(buffer);
这是我的代码
byte[] bytearray = null;
using (var ms = new MemoryStream())
{
if (bmp != null)
{
var wbitmp = new WriteableBitmap(bmp);
wbitmp.SaveJpeg(ms, 46, 38, 0, 100);
bytearray = ms.ToArray();
}
}
if (bytearray != null)
{
// image base64 here
string btmStr = Convert.ToBase64String(bytearray);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.