C# Awesomium Webview表面到字节缓冲区或PictureBox

C# Awesomium Webview表面到字节缓冲区或PictureBox,c#,.net,c#-4.0,awesomium,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(byte

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

方法呢?这应该是你想要的。

方法呢?这应该是你想要的。

最近,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();

资料来源:

最近,冬虫夏草有许多未经记录的变化

尝试
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();

来源:

你的意思是这样的

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();
    }
}

你是说像这样的

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();
    }
}

如果我错了,请纠正我,但在较低的级别上,图像只是一个字节数组。你能从
BitmapSurface
中获取字节并从中创建一个
系统.绘图.图像
吗?我想这样做,但我不知道怎么做:“
surface
变量?缓冲区(指向原始像素缓冲区的指针(32位BGRA格式,4 bpp)。”作为IntPtr--其他:SaveToPNG,SaveToJPEG,高度、宽度、视图…如果我错了,请纠正我,但在较低的级别上,图像只是一个字节数组。你能从
BitmapSurface
中获取字节并从中创建一个
系统.绘图.图像
吗?我想这样做,但我不知道怎么做:“
surface
变量?缓冲区(指向原始像素缓冲区的指针(32位BGRA格式,4 bpp)。”作为IntPtr--其他:SaveToPNG,SaveToJPEG,高度、宽度、视野……你能告诉我怎么做吗?缓冲区是一个IntPtr。。。但是我不知道如何使用它,可能需要看看Marshal类。从未使用过它,但您可能需要将缓冲区返回值转换为带符号的32位整数,并使用类似以下内容
Marshal.ReadByte(IntegerPointer)Marshal.ReadByte(IntegerPointer)从Awesomium 1.7.5开始,此解决方案不再有效。似乎(1)
WebCore.Update()
已过时,并且
(位图表面)vw.Surface).SaveToJPEG(“D:\\google.jpg”)给出错误:
未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例
。我正试图找出如何使它与他们的更新包一起工作,但文档非常稀少。以下是更新的示例:从Awesomium 1.7.5开始,此解决方案不再工作。似乎(1)
WebCore.Update()
已过时,并且
(位图表面)vw.Surface).SaveToJPEG(“D:\\google.jpg”)给出错误:
未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例
。我正试图找出如何使其与更新的软件包一起工作,但文档非常稀少。以下是更新的示例: