1

我开始用 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.??
4

4 回答 4

8

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

于 2013-03-19T23:08:19.790 回答
2

你的意思是这样的?

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();
    }
}
于 2013-05-30T06:58:01.567 回答
2
  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;
于 2013-10-17T01:57:13.950 回答
-1

那么Buffer方法呢?那应该是你想要的。

于 2013-03-01T21:26:18.180 回答