Search code examples
c#.netc#-4.0awesomium

Awesomium Webview Surface to Byte Buffer or PictureBox


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.??

Solution

  • 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