[英]Use local images in Webbrowser control
我在Wp7应用程序中使用了Webbrowser控件,但似乎无法将Web浏览器中App目录中的图像放入。
我已经将一些图像放在与.cs和.xaml文件位于同一目录的文件夹中。 现在,我尝试将其放入Web浏览器控件中,但似乎无法正常工作。
<img src="images/photo.jpg"/>
<img src="/images/photo.jpg"/>
上面的两个显然不起作用,我猜应该是这样的:
<img src="/SilverlightApplication;component/photo.jpg"/>
“ SilverlightApplication”和“ component”应替换为其他内容,但我不知道什么:(
您将需要将图像存储在隔离存储中,然后从那里显示图像。 我整理了一个样本,您可以从以下位置下载:-
www.smartmobiledevice.co.uk/projects/webbrowserimagesample.zip
这基于MSDN文章如何:使用Windows Phone的WebBrowser控件显示静态Web内容 。
在Windows Phone 8上,其中提供了一些WinRT类,您可以获取应用程序独立存储的文件系统路径。 因此,IsoStorage中文件的绝对URL为:
string URL = "file://" +
Windows.Storage.ApplicationData.Current.LocalFolder.Path +
"\\folder\\filename.png";
WebBrowser控件可以在NavigateToString()
HTML中接受此类URL。 或者,您可以将IsoStorage指定为基础,并始终使用相对URL。 isostore:
URL无效,我试过了。 ms-appx://local/
。
为了完整起见,您可以非常类似地获取应用程序资源的文件系统路径。 那将是Windows.ApplicationModel.Package.Current.InstalledLocation.Path
。
很好,您可以通过连接上面给定的Paul示例应用程序动态使用数组来使用动态图像,
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();
}
}
这是一个非常老的线程,但是我想出了一个解决方案,因为我们今天才更新了WP7应用程序。
秘诀是首先将图像转换为基数为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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.