簡體   English   中英

將ImageSource轉換為Base64String - WP8

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM