Convert ImageSource to Base64String - WP8

只愿长相守 提交于 2020-01-06 14:16:49

问题


am working on WP8application, I have few images in location Resources\Graphics\ i am trying to display images from these folder, but its not picking the path.

Here is my code :

<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.

But its not display the images.

So i want to convert the ImageSource --Resources;component/Graphics/"+ImageName to Base64String how to do it?

I have looked into many examples but none of them is compatible for WP8.


回答1:


You can get StreamInfo by using this:

Application.GetResourceStream(new Uri("Resources;component/Graphics/"+ImageName", System.UriKind.Relative));

Then you can read this stream into an byte array. After that, use Convert.ToBase64String() to get what you want. Try this. Maybe you can read the MSDN document to find how to use 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:


It's very simple - load your image into a byte array and call

System.Convert.ToBase64String(imageArray).

That being said, this will not result in displaying the image. The NavigateToString requires html. See documentation




回答3:


This is my code

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


来源:https://stackoverflow.com/questions/23366212/convert-imagesource-to-base64string-wp8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!