将ImageSource转换为Base64String - WP8

时间:2014-04-29 13:33:48

标签: c# windows-phone-8 windows-phone base64 imagesource

正在研究WP8应用程序,我在位置上的图像很少资源\图形\我正在尝试显示这些文件夹中的图像,但它没有选择路径。

这是我的代码:

<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兼容。

3 个答案:

答案 0 :(得分:1)

非常简单 - 将图像加载到字节数组中并调用

System.Convert.ToBase64String(imageArray)

话虽这么说,但这不会导致显示图像。 NavigateToString需要html。见documentation

答案 1 :(得分:1)

您可以使用以下内容获取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);

答案 2 :(得分:0)

这是我的代码

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