Windows Phone 用WebBrowser加载本地图片以及解决加载html页面出现乱码问题

1.首先,将图片保存至独立存储

 private void SaveFilesToIsoStore()
{
     string[] files = { "1.jpg" };
     IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
     if (false == isoStore.FileExists(files[0]))
     {
     foreach (string f in files)
     {
          StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
          using (BinaryReader br = new BinaryReader(sr.Stream))
          {
               byte[] data = br.ReadBytes((int)sr.Stream.Length);
               SaveToIsoStore(f, data);
           }
       }
     }
}
private void SaveToIsoStore(string fileName, byte[] data)
        {
            string strBaseDir = string.Empty;
            string delimStr = "/";
            char[] delimiter = delimStr.ToCharArray();
            string[] dirsPath = fileName.Split(delimiter);

            //Get the IsoStore.
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            //Re-create the directory structure.
            for (int i = 0; i < dirsPath.Length - 1; i++)
            {
                strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
                isoStore.CreateDirectory(strBaseDir);
            }

            //Remove the existing file.
            if (isoStore.FileExists(fileName))
            {
                isoStore.DeleteFile(fileName);
            }

            //Write the file.
            using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
            {
                bw.Write(data);
                bw.Close();
            }
        }
private void AnotherWayToReferImageInIso()
        {
            //create html
            string htmlPath = "index.htm";

            string html = "<div class=\"mid_r_content\">"
                          + "<div id=\"contentBar1\">"
                          + " <div align=\"left\" class=\"div_news\">"
                          + "<img alt=\"\" width=\"310\" src=\"1.jpg\">"
                          + "</div>"
                          + "</div>"
                          + "</div>";
            string newHtml = ConvertExtendedASCII(html);

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(htmlPath))
                {
                    store.DeleteFile(htmlPath);
                }
                using (var stream = store.CreateFile(htmlPath))
                {
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(newHtml);
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            webBrowser1.Navigate(new Uri(htmlPath, UriKind.RelativeOrAbsolute));
        }

 

//解决乱码问题
        public static string ConvertExtendedASCII(string HTML)
        {
            StringBuilder str = new StringBuilder();
            char c;
            for (int i = 0; i < HTML.Length; i++)
            {
                c = HTML[i];
                if (Convert.ToInt32(c) > 127)
                {
                    str.Append("&#" + Convert.ToInt32(c) + ";");
                }
                else
                {
                    str.Append(c);
                }
            }
            return str.ToString();
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值