WebBrowser c#object:将google chrome设置为默认浏览器

时间:2017-06-03 10:23:49

标签: c# webbrowser-control screenshot

使用以下代码捕获思维导图网址的屏幕截图:

using (WebBrowser browser = new WebBrowser())
            {                
                browser.Width = 900;
                browser.Height = 900;
                browser.ScrollBarsEnabled = true;

                // This will be called when the page finishes loading
                browser.DocumentCompleted += Program.OnDocumentCompleted;

                browser.Navigate("https://app.mindmup.com/map/_free/2017/06/4e6750b0484011e7a155fd63d7b56500");

                // This prevents the application from exiting until
                // Application.Exit is called
                Application.Run();
            }

但是,无法在IE中打开思维导图网址,因此无法捕获屏幕截图。任何人都可以建议如何在使用Web浏览器对象时将谷歌浏览器设置为浏览器。或者其他一些代码来捕捉截图

1 个答案:

答案 0 :(得分:0)

您无法更改WebBrowser使用的浏览器。但是,还有其他一些解决方案:

<强> 1。使用其他Web浏览器

我会推荐CefSharp。它使用Chromium,非常适合您的项目。

有关CefSharp的更多信息:here

<强> 2。强制WebBrowser运行IE11

默认情况下,WebBrowser使用Internet Explorer 7。幸运的是,您可以强迫它选择更新的版本。

在加载表单之前,首先运行createRegistry()。您可以在Program.cs中调用它。如果要还原注册表,请运行removeRegistry()。

public static void createRegistry()
    {
        string filename = Process.GetCurrentProcess().MainModule.FileName;
        filename = filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - filename.LastIndexOf('\\') - 1);
        if (filename.Contains("vhost"))
            filename = filename.Substring(0, filename.IndexOf('.') + 1) + "exe";

            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true).SetValue(filename, 11001, RegistryValueKind.DWord);

            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BEHAVIORS", true).SetValue(filename, 11001, RegistryValueKind.DWord);

            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true).SetValue(filename, 11001, RegistryValueKind.DWord);

            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BEHAVIORS", true).SetValue(filename, 11001, RegistryValueKind.DWord);
    }

    public static void removeRegistry()
    {
        string filename = Process.GetCurrentProcess().MainModule.FileName;
        filename = filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - filename.LastIndexOf('\\') - 1);
        if (filename.Contains("vhost"))
            filename = filename.Substring(0, filename.IndexOf('.') + 1) + "exe";
        try
        {
            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true).DeleteValue(filename);
        }
        catch
        {

        }
        try
        {
            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BEHAVIORS", true).DeleteValue(filename);
        }
        catch
        {

        }
        try
        {
            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true).DeleteValue(filename);
        }
        catch
        {

        }
        try
        {
            Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BEHAVIORS", true).DeleteValue(filename);
        }
        catch
        {

        }
    }