Awesomium Webview Surface to Byte Buffer或PictureBox

时间:2013-03-01 20:53:34

标签: c# .net c#-4.0 awesomium

我开始在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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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();
    }
}

答案 2 :(得分: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;

答案 3 :(得分:-1)

Buffer方法怎么样?这应该是你想要的。