C# 在Webbrowser控件中使用本地图像
我在Wp7应用程序中使用Webbrowser控件,但我似乎无法将应用程序目录中的图像放入Webbrowser 我已将一些图像放在与.cs和.xaml文件相同的目录下的文件夹中。现在我试着把它们放在webbrowser控件中,但我似乎无法让它工作C# 在Webbrowser控件中使用本地图像,c#,windows-phone-7,xaml,webbrowser-control,C#,Windows Phone 7,Xaml,Webbrowser Control,我在Wp7应用程序中使用Webbrowser控件,但我似乎无法将应用程序目录中的图像放入Webbrowser 我已将一些图像放在与.cs和.xaml文件相同的目录下的文件夹中。现在我试着把它们放在webbrowser控件中,但我似乎无法让它工作 <img src="images/photo.jpg"/> <img src="/images/photo.jpg"/> 上面两个显然不起作用,我猜应该是这样的: <img src="/SilverlightApplica
<img src="images/photo.jpg"/>
<img src="/images/photo.jpg"/>
上面两个显然不起作用,我猜应该是这样的:
<img src="/SilverlightApplication;component/photo.jpg"/>
SilverlightApplication和组件应该被其他东西替换,但我不知道是什么:您需要将图像存储在独立的存储中,然后从那里显示图像。我制作了一个示例,您可以从以下位置下载:-
这是基于MSDN文章的。通过连接上述示例应用程序,可以使用动态图像动态更新阵列
string[] files = {
"readme.htm", "desert.jpg", "sample.jpg"
};
在写入之前,您可以删除现有的一个
private void SaveFilesToIsoStore()
{
//These files must match what is included in the application package,
//or BinaryStream.Dispose below will throw an exception.
string[] files = {
"readme.htm", "desert.jpg", "sample.jpg"
};
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if(isoStore.FileExists(files[0]))
{
isoStore.DeleteFile(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();
}
}
在Windows Phone 8上,有些WinRT类可用,您可以获得应用程序隔离存储的文件系统路径。因此,IsoStorage中文件的绝对URL为:
string URL = "file://" +
Windows.Storage.ApplicationData.Current.LocalFolder.Path +
"\\folder\\filename.png";
WebBrowser控件以导航字符串的HTML格式接收此类URL。或者您可以将IsoStorage指定为基础,并在整个过程中使用相对URL。isostore:url不起作用,我试过了。微软也没有-appx://local/.
为了完整起见,您可以非常类似地获得应用程序资源的文件系统路径。那就是Windows.ApplicationModel.Package.Current.InstalledLocation.Path。这是一个非常古老的线程,但我提出了一个解决方案,因为我们今天刚刚更新了一个WP7应用程序 秘密在于首先将图像转换为base 64表示,因此请从以下代码开始:
private string GetBase64(string f)
{
string ret = "";
{
StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
using (BinaryReader br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
ret = System.Convert.ToBase64String(data);
}
}
return ret;
}
现在,你想在我的代码中注入图像的地方是GIF使用的吗
StringBuilder sb = ... // a string builder holding your webpage
String b64 = GetBase64("assets/images/filename.gif");
sb.AppendFormat(@"<img src='data:image/gif;base64,{0}' /></div>", b64);
非常感谢你,保罗!但是我在Webbrowser上使用了NavigateToString方法,并将整个html页面放在字符串中。由于某种原因,如果我这样做,图像就不起作用了Sbtw。只有独立故事中的图像不适用于NavigateToString。具有绝对Uri的IAMGE可以工作。您还需要将字符串保存到独立存储中的文件中,并将WebBrowser控件上的Base属性设置为保存文件html、图像、css、,javascriptHi@Paul有任何方法可以使用NavigateToString加载本地图像,因为我有多个图像需要根据用户选择动态加载图像伟大的答案。与NavigateToString完美配合。非常感谢:-@SevaAlekseyev重要提示:上述技术不适用于存储中与本地文件夹同级的其他目录,仅适用于本地文件夹及其以下的目录。例如,我使用Path.GetTempPath方法创建了一个名为Temp的本地同级目录。WebBrowser组件无法访问该目录中的文件。将它们复制到本地文件夹解决了问题。