我开始使用 C# 进行开发,我正在尝试将表面转换为字节缓冲区或图片(之后也转换为字节缓冲区)。
我在其他问题中看到这段代码:
string fileName = Path.GetTempFileName();
webView2.Render().SaveToPng(fileName);
byte[] bytes = File.ReadAllBytes(fileName);
File.Delete(fileName);
MemoryStream ms = new MemoryStream(bytes);
但是,一个 webview 没有 Render(),他也没有说我需要导入什么库。
我到此为止:
var view = (WebView)WebCore.Views.Last();
WebCore.Update();
BitmapSurface surface = (BitmapSurface)view.Surface;
surface.??
请您参考如下方法:
Awesomium 最近发生了许多未记录的更改。
尝试使用 WebView.Surface 而不是 WebView.Render。
using (WebView vw = WebCore.CreateWebView(1024, 768)) {
vw.Source = new Uri("http://www.google.com");
while (vw.IsLoading) {
WebCore.Update();
}
((BitmapSurface)vw.Surface).SaveToJPEG("D:\\google.jpg");
PictureBox1.Load("D:\\google.jpg");
WebCore.Shutdown();
}
评论中指出了另一组更改。为了正确起见,这里提供了更新后的代码和文档链接。
using ( webView = WebCore.CreateWebView( 800, 600 ) )
{
webView.Source = new Uri( "http://www.google.com" );
view.LoadingFrameComplete += ( s, e ) =>
{
if ( !e.IsMainFrame )
return;
BitmapSurface surface = (BitmapSurface)view.Surface;
surface.SaveToPNG( "result.png", true );
WebCore.Shutdown();
}
}
WebCore.Run();
来源:http://docs.awesomium.net/html/b2fc3fe8-72bd-4baf-980f-b9b9456d5ca4.htm
