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