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