Awesomium Webview Surface to Byte Buffer or Pictur

2019-06-03 16:41发布

I begun to develope in C#, and I'm trying to convert a surface to a byte buffer or to a Picture (to convert after to a byte buffer too).

I saw in other question this code:

string fileName = Path.GetTempFileName();
webView2.Render().SaveToPng(fileName);
byte[] bytes = File.ReadAllBytes(fileName);
File.Delete(fileName);
MemoryStream ms = new MemoryStream(bytes);

But, a webview don't have Render(), and he don't say what libraries I need to import.

I stop here:

var view = (WebView)WebCore.Views.Last();
WebCore.Update();
BitmapSurface surface = (BitmapSurface)view.Surface;
surface.??

4条回答
做自己的国王
2楼-- · 2019-06-03 17:24
  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;
查看更多
成全新的幸福
3楼-- · 2019-06-03 17:25

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

查看更多
一纸荒年 Trace。
4楼-- · 2019-06-03 17:28

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();
    }
}
查看更多
家丑人穷心不美
5楼-- · 2019-06-03 17:37

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

查看更多
登录 后发表回答