调整Awesomium WebView的大小不会调整表面大小

时间:2015-06-05 13:24:42

标签: c# awesomium

我使用awesomium拍摄HTML页面的快照。

public Bitmap renderHtml(string htmlContent)
    {
        using (WebSession session = WebCore.CreateWebSession(WebPreferences.Default))
        {
            int docHeight = 0, docWidth = 0;
            using (WebView view = WebCore.CreateWebView(1, 1, WebViewType.Offscreen))
            {

                bool finishedLoading = false;
                var uri = new Uri("data:text/html," + htmlContent, UriKind.Absolute);
                //uri = new Uri("http://www.google.com");
                view.Source = uri;
                view.LoadingFrameComplete += (s, e) =>
                    {
                        if (e.IsMainFrame)
                        {
                            finishedLoading = true;
                            docHeight = (int)view.ExecuteJavascriptWithResult("(function() { var bodyElmnt = document.body; var html = document.documentElement; var height = Math.max( bodyElmnt.scrollHeight, bodyElmnt.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); return height; })();");
                            docWidth = (int)view.ExecuteJavascriptWithResult("(function() { var bodyElmnt = document.body; var html = document.documentElement; var width = Math.max( bodyElmnt.scrollWidth, bodyElmnt.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth); return width; })();");
                        }
                    };
                while (!finishedLoading)
                {
                    Thread.Sleep(100);
                    WebCore.Update();
                }
                //view.Surface.Initialize(view, docWidth, docHeight);
                view.Resize(docWidth, docHeight);

                //cannot do this here because view.Surface size is (1,1)
                /*
                bmp = new Bitmap(docWidth, docHeight);
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
                BitmapSurface bmpSurface = (BitmapSurface)view.Surface;
                bmpSurface.CopyTo(bmpData.Scan0, bmpSurface.RowSpan, 4, false, false);
                bmp.UnlockBits(bmpData);
                */ 
            }
            using (WebView view2 = WebCore.CreateWebView(docWidth, docHeight, WebViewType.Offscreen))
            {
                bool finishedLoading = false;
                var uri = new Uri("data:text/html," + htmlContent, UriKind.Absolute);

                view2.Source = uri;
                view2.LoadingFrameComplete += (s, e) =>
                {
                    if (e.IsMainFrame)
                    {
                        finishedLoading = true;
                    }
                };
                while (!finishedLoading)
                {
                    Thread.Sleep(100);
                    WebCore.Update();
                }
                bmp = new Bitmap(docWidth, docHeight);
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
                BitmapSurface bmpSurface = (BitmapSurface)view2.Surface;
                bmpSurface.CopyTo(bmpData.Scan0, bmpSurface.RowSpan, 4, false, false);
                bmp.UnlockBits(bmpData);

            }
        }

        return bmp;
    }

这是有效的,但是我需要使用从第一个视图获得的页面大小来创建第二个视图,因为当我调用view.Resize()时它不会调整视图的Surface大小,所以我可以使用它。因为尺寸不同而导致尝试保存图像时出错。如果我先使用新的宽度和高度调用Surface.Initialize,那么原始曲面就会消失,而我得到的只是一个空的白色图像。

如何在不创建第二个视图的情况下调整视图表面的大小并使其工作?

1 个答案:

答案 0 :(得分:2)

在调整WebView大小后,您无法立即使用WebView的Surface,因为它需要一些时间来调整大小,更新和重绘。更新是异步完成的,因此您应该等待Surface.Resized事件发生。尝试类似的东西:

<td class="TdCenter EditPermissionName">

此代码片段将打开一个网站,截取屏幕截图,然后调整您的网页浏览大小,等待调整大小以触发并拍摄另一个屏幕截图。

希望有所帮助。