Awesomium Webview Surface to Byte Buffer or PictureBox

大憨熊 提交于 2019-12-08 14:29:45

There have been many undocumented changes in Awesomium lately.

Try WebView.Surface instead of 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();
}

There have been another set of changes that were pointed out in the comments. Just for the sake of correctness, here is an updated code and a link to the documentation.

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

Source: http://docs.awesomium.net/html/b2fc3fe8-72bd-4baf-980f-b9b9456d5ca4.htm

You mean something like this?

private static byte[] getWebViewScreenshotAsBytes(ref WebView myWebView)
{
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
        using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myWebView.Width, myWebView.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
            BitmapSurface bmpSurface = (BitmapSurface)myWebView.Surface;
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
            bmpSurface.CopyTo(bmpData.Scan0, bmpSurface.RowSpan, 4, false, false);
            bmp.UnlockBits(bmpData);
            bmp.Save(ms, ImageFormat.Png);
        }
        return ms.ToArray();
    }
}
  double width = 800;
  double height = 1000;
  var webView = WebCore.CreateWebView(width, height, WebViewType.Offscreen);
  webView.Source = new Uri("https://www.google.com/");
  while (webView.IsLoading)
  {
    WebCore.Update();
  }
  var bitmapSurface = (BitmapSurface)webView.Surface;
  var writeableBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
  writeableBitmap.Lock();
  bitmapSurface.CopyTo(writeableBitmap.BackBuffer, bitmapSurface.RowSpan, 4, false, false);
  writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
  writeableBitmap.Unlock();
  var image = new Image();
  image.Source = writeableBitmap;

What about the Buffer method? That should be what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!