询问者
关于WebBrowser截不了图的问题, 急,请帮忙解答!谢谢!

常规讨论
-
使用Webbrowser作为网页浏览控件,需要实现一项截取当前用户浏览内容的截图保存下来。 目前使用WriteableBitmap来对Webbrowser截图,发现拦截下来的图片里面,什么东西都没有,但实际页面有内容。 在网络上找了一下,说这个是一个BUG! 附上我的代码,请帮忙看下,如果是一个BUG,请问什么时候能够解决呢,或者有没有其他的方法能对Webbrowser截图吗? 截图部分的代码: WriteableBitmap bitmap = new WriteableBitmap(webbrowser,null); MemoryStream ms = new MemoryStream(); bitmap.SaveJpeg(ms, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100); ms.Seek(0, SeekOrigin.Begin); byte[] binaryData = new byte[ms.Length]; ms.Read(binaryData, 0, binaryData.Length); System.IO.IsolatedStorage.IsolatedStorageFile file = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); FileStream filestream = new System.IO.IsolatedStorage.IsolatedStorageFileStream(@"image.jpg", FileMode.Create, file); string[] directoriesInTheRoot = file.GetDirectoryNames(); filestream.Write(binaryData, 0, binaryData.Length); filestream.Flush(); filestream.Close();
- 已移动 Jiong ShiMVP 2011年4月2日 2:01 (发件人:Windows Phone 用户)
- 已编辑 vforkk 2011年4月8日 3:27
- 已更改类型 MagicBoy-PengModerator 2011年4月13日 5:44
全部回复
-
1.首先验证通过WriteableBitmap能否对WebBrowser控件抓图
我尝试在Silverlight Application和Silverlight for WindowsPhone Application中抓图功能。
相同的代码在Silverlight Application可以实现对WebBrowser的Snapshot(抓图)。在Silverlight for WindowsPhone Application中无法实现对WebBrowser的Snapshot(抓图)。
代码如下:
Silverlight Application
Project: Silverlight Application File: MainPage.xaml
<Grid x:Name="LayoutRoot" Background="White">
<WebBrowser Height="100" HorizontalAlignment="Left" Margin="61,26,0,0" Name="webBrowser1" VerticalAlignment="Top" Width="200" Source="http://www.bing.com" />
<Image Height="150" HorizontalAlignment="Left" Margin="61,138,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="300,70,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
Project: Silverlight Application File: MainPage.xaml.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
WriteableBitmap wb = new WriteableBitmap(webBrowser1, null);
image1.Source = wb;
}
在Silverlight使用WriteableBitmap创建WebBrowser的Snapshot成功。
Silverlight for WindowsPhone Application
Project: Silverlight for WindowsPhone Application File: MainPage.xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:WebBrowser HorizontalAlignment="Left" Margin="100,71,0,0" Name="webBrowser1" VerticalAlignment="Top" Height="181" Width="269" Source="http://www.bing.com" />
<Image Height="150" HorizontalAlignment="Left" Margin="100,399,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="282" />
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="159,294,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
</Grid>
Project: Silverlight for WindowsPhone Application File: MainPage.xaml.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
WriteableBitmap wb = new WriteableBitmap(webBrowser1, null);
image1.Source = wb;
}
在Windows Phone使用WriteableBitmap创建WebBrowser的Snapshot失败。
即问题出在系统是否支持对WebBrowser控件的截图功能。
2. 在Windows Phone尝试对其他控件的抓图。
经过尝试,在Windows Phone中支持对Image控件的抓图。
-
这个链接中有人问了和您同样的问题
从回答的结果来看,WebBrowser控件还没有提供截图(screenshot)的功能。
Cedar -
Using Javascript in WebBrowser in Silverlight for Windows Phone,
也许能解决您的问题。
在WebBrowser中使用Javascript需要设置WebBrowser 控件的属性,IsScriptEnabled属性应为有效.
下面的链接对您也许有用
Javascript in WebBrowser in Silverlight in Windows Phone
(http://social.msdn.microsoft.com/forums/en-us/windowsphone7series/thread/F4BDB101-E96C-463E-95F3-D421A4C02B64)
Cedar -
You have to using WebBrowser and InvokeScript to call Javascript methods .
请参考using WebBrowser and InvokeScript to call Javascript methods to extract tokens
(http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/df60628f-62dd-452f-99d9-b05e3671161f).
Cedar -
Windows Phone 7 Mango系统的更新
开发工具更新(下月更新)
- New Template for Multitasking; Debugging Background Agents; Isolate storage explorer; Profiler; VB in Express;
- .Net: Performance; Generational GC; Serialization; SIMD-Vector; Profiler;
- Emulator: Sensors & location; Multi touch; Screenshot; Ingestion tool;
开发工具的更新包括Screenshot和对于Sivlerlight 4增强,我想新的开发工具将会解决您的问题,共同期待吧。
Cedar