zoukankan      html  css  js  c++  java
  • CefSharp.OffScreen.Example

    namespace CefSharp.OffScreen.Example
    {
        public class Program
        {
            private static ChromiumWebBrowser browser;
    
            public static void Main(string[] args)
            {
                const string testUrl = "http://www.baidu.com/";
    
                Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", testUrl);
                Console.WriteLine("You may see a lot of Chromium debugging output, please wait...");
                Console.WriteLine();
    
                // You need to replace this with your own call to Cef.Initialize();
                CefExample.Init();
    
                // Create the offscreen Chromium browser.
                browser = new ChromiumWebBrowser();
    
                // An event that is fired when the first page is finished loading.
                // This returns to us from another thread.
                browser.FrameLoadEnd += BrowserFrameLoadEnd;
    
                // Start loading the test URL in Chrome's thread.
                //browser.Load(testUrl);
                browser.Load("dummy:");
    
                // We have to wait for something, otherwise the process will exit too soon.
                Console.ReadKey();
    
                // Clean up Chromium objects.  You need to call this in your application otherwise
                // you will get a crash when closing.
                Cef.Shutdown();
            }
    
            private static void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
            {
                ChromiumWebBrowser browser = sender as ChromiumWebBrowser;
                if (browser.Title == "dummy:")
                {
                    browser.LoadHtml("<html><head></head><body><h1>OKOKOK</h1></body></html>", "about:blank");
                    return;
                }
    
    
                // Check to ensure it is the main frame which has finished loading
                // (rather than an iframe within the main frame).
                if (e.IsMainFrame)
                {
                    // Remove the load event handler, because we only want one snapshot of the initial page.
                    browser.FrameLoadEnd -= BrowserFrameLoadEnd;
    
                    var task1 = browser.GetSourceAsync();
                    task1.Wait();
                    string html= task1.Result;
    
                    // Wait for the screenshot to be taken.
                    var task = browser.ScreenshotAsync();
                    task.Wait();
    
                    // Make a file to save it to (e.g. C:UsersjanDesktopCefSharp screenshot.png)
                    var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
    
                    Console.WriteLine();
                    Console.WriteLine("Screenshot ready.  Saving to {0}", screenshotPath);
    
                    // Save the Bitmap to the path.
                    // The image type is auto-detected via the ".png" extension.
                    task.Result.Save(screenshotPath);
    
                    // We no longer need the Bitmap.
                    // Dispose it to avoid keeping the memory alive.  Especially important in 32-bit applications.
                    task.Result.Dispose();
    
                    Console.WriteLine("Screenshot saved.  Launching your default image viewer...");
    
                    // Tell Windows to launch the saved image.
                    Process.Start(screenshotPath);
    
                    Console.WriteLine("Image viewer launched.  Press any key to exit.");
                }
            }
        }
    }
    

      

  • 相关阅读:
    js禁止鼠标右键功能
    js判断客户端是pc还是手机及获取浏览器版本
    js实现深拷贝的一些方法
    python使用requests请求的数据乱码
    PyCharm引入python需要使用的包
    几个常见用于解决nginx负载均衡的session共享问题的办法
    面试最让你手足无措的一个问题:你的系统如何支撑高并发?
    Linux/Windows 平台最容易安装 Composer教程
    Laravel一些常用命令整理
    nginx下重写隐藏index.php文件
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7218976.html
Copyright © 2011-2022 走看看