HTML5 on WebBrowser works slower than IE or other browser - c#

I need to show HTML5 pages for WinForm and use component Webbrowser in mode emulation IE10.
When I open any hard page in Webbrowser I see it works slower than IE or other browsers.
For example
Uri uri = new Uri("http://createjs.com/demos/easeljs/Cache.html");
webBrowser.Url = uri;
webBrowser.Navigate(uri);
At http://createjs.com/demos/easeljs/Cache.html page if set "cache enabled" fps in IE10 > 60 but Webbrowser shows only 15 fps
Registry Mode Emulation IE10 for WebBrowser
private static void Emulation() // IE10
{
try
{
string keyName = #"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
string valueName = System.AppDomain.CurrentDomain.FriendlyName;
RegistryKey key = Registry.LocalMachine.OpenSubKey(keyName, false);
object FindKey = Registry.GetValue("HKEY_LOCAL_MACHINE\\"+ keyName,System.AppDomain.CurrentDomain.FriendlyName,null);
if (FindKey == null)
{
RegistryKey RegistryKey = Registry.LocalMachine.CreateSubKey(keyName);
RegistryKey.SetValue(System.AppDomain.CurrentDomain.FriendlyName, 10001, RegistryValueKind.DWord);
}
}
catch (Exception Ex)
{
Console.WriteLine("Registry update error: " + Ex.Message);
Console.WriteLine("Can't change browser version.");
}
}
Any advice how to increase performance WebBrowser on WinForm?

What about using another Default Browser?
https://cefsharp.github.io/
Cefsharp is a port of Chromium to .NET! You can use it instead of IE...
Or you could this:
http://www.awesomium.com/#download
Awesomium is a HTML Engine...

You must set in the registry to use the latest IE version or Edge. Otherwise the oldest IE7 will be used.
Use latest version of Internet Explorer in the webbrowser control

When I updated IE from 10 to 11 version performance in component WebBrowser increased to the normal value.

Related

Display WebPage in C#

I want to display The webpage: http://vhg.cmp.uea.ac.uk/tech/jas/vhg2018/WebGLAv.html in Windows Form of my C# project.
I use WebBrowser tool in it and write this code to show this webpage in the Form:
webBrowser1.Navigate("http://vhg.cmp.uea.ac.uk/tech/jas/vhg2018/WebGLAv.html");
but it doesn't show the full webpage!
What should I've done?
By default Windows Forms applications use IE wrapper and not guarantee to use latest IE version. Read this article to know what happen behind IE wrappers and what Windows emulation key do.
This code from one of my old project let to change the default emulation version of IE for your executable process programmatically :
private static readonly string BrowserEmulationRegistryKeyPath =
#"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
/// <summary>
/// Add the process name to internet explorer emulation key
> /// i do this because the default IE wrapper dose not support the latest JS features
> /// Add process to emulation key and set a DWord value (11001) for it means we want use IE.11 as WebBrowser component
/// </summary>
public bool EmulateInternetExplorer()
{
using (
var browserEmulationKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationRegistryKeyPath,
true))
{
if (browserEmulationKey == null)
Registry.CurrentUser.CreateSubKey(BrowserEmulationRegistryKeyPath);
string processName = $"{Process.GetCurrentProcess().ProcessName}.exe";
// Means emulation already added and we are ready to start
if (browserEmulationKey?.GetValue(processName) != null)
return true;
// Emulation key not exists and we must add it ( We return false because application restart to take effect of changes )
if (browserEmulationKey != null)
{
browserEmulationKey.SetValue(processName, 11001, RegistryValueKind.DWord);
browserEmulationKey.Flush();
}
return false;
}
}
If your website dose not show properly in latest Internet Explorer (not compatible) you should use other web browser wrappers like cefSharp that embed Chromium in your .Net app.
you can use google SDK to browse or using showing html content referee this
link

C# webBrowser script error

I keep getting a script error when trying to load the page using webBrowser.Navigate("https://home.nest.com/"). It will pull up fine from my normal internet browser but not in my program.
Can anyone point me in the right direction?
as this link answer:
you must only add this line:
webBrowser.ScriptErrorsSuppressed = true;
The script errors happen all of the time in the integrated Internet Explorer WebBrowser control even when it's using version 11. Modern websites rely heavily on massive Javascript files and dynamic rendering. You can see that just by watching that page load in a regular browser. The control just can't cut it some of the times.
You might want to try some alternative browser controls. There are no guarantees that it will work with any of them, but at least it's something to try.
Awesomium : Originally based on Chromium. I don't know if they still integrate Chromium changes or if they've gone in their own direction. It's free for personal use as well as commercial making less than $100k.
DotNetBrowser : Embed a Chromium-based WPF / WinForms component into your .NET application to display modern web pages built with HTML5, CSS3, JavaScript, Silverlight etc.
geckofx : An open-source component for embedding Mozilla Gecko (Firefox) in .NET applications.
Xilium.CefGlue : A .NET/Mono binding for The Chromium Embedded Framework (CEF) by Marshall A. Greenblatt.
BrowseEmAll : BrowseEmAll.Cef (Chrome), BrowseEmAll.Gecko (Firefox), BrowseEmAll Core API (Chrome,Firefox,IE - COMMERCIAL)
There are probably others, but this should give you a start with some of the more popular active projects if you want to pursue this route.
The WebBrowser control is capable of rendering most web pages, but by default it attempts to render pages in compatibility mode (pretty much IE7, hence the issues). If you are building your own page, it's simple, just add the following tag to the header and it should render fine...
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
However, if you are trying to render a third party site you cannot add tags to, things get more difficult. As mentioned above, you can use a registry key (HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION) if it's just on your own machine.
If neither of these options are a possible solution, using a different browser control (again, great suggestions above) is pretty much your only option.
There's a great blog on controlling the browser control compatibility mode at https://learn.microsoft.com/en-gb/archive/blogs/patricka/controlling-webbrowser-control-compatibility
You should add your program name to the register HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
for using the latest feature as same as your normal internet browser.
as for me, value 8000 (0x1F40) - IE8 mode can solve many script error problem.
Ref:
Use latest version of Internet Explorer in the webbrowser control
private void Form1_Load(object sender, EventArgs e)
{
var appName = Process.GetCurrentProcess().ProcessName + ".exe";
SetIE8KeyforWebBrowserControl(appName);
webBrowser1.ScriptErrorsSuppressed = true;
}
private void SetIE8KeyforWebBrowserControl(string appName)
{
RegistryKey Regkey = null;
try
{
// For 64 bit machine
if (Environment.Is64BitOperatingSystem)
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(#"SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
else //For 32 bit machine
Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(#"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
// If the path is not correct or
// if the user haven't priviledges to access the registry
if (Regkey == null)
{
MessageBox.Show("Application Settings Failed - Address Not found");
return;
}
string FindAppkey = Convert.ToString(Regkey.GetValue(appName));
// Check if key is already present
if (FindAppkey == "8000")
{
MessageBox.Show("Required Application Settings Present");
Regkey.Close();
return;
}
// If a key is not present add the key, Key value 8000 (decimal)
if (string.IsNullOrEmpty(FindAppkey))
Regkey.SetValue(appName, unchecked((int)0x1F40), RegistryValueKind.DWord);
// Check for the key after adding
FindAppkey = Convert.ToString(Regkey.GetValue(appName));
if (FindAppkey == "8000")
MessageBox.Show("Application Settings Applied Successfully");
else
MessageBox.Show("Application Settings Failed, Ref: " + FindAppkey);
}
catch (Exception ex)
{
MessageBox.Show("Application Settings Failed");
MessageBox.Show(ex.Message);
}
finally
{
// Close the Registry
if (Regkey != null)
Regkey.Close();
}
}
You may even set the registry value to 11000 to have the latest version of IE!!

How to set IE9 by default for web browser?

I am developing an application in C#.NET. I want to use the IE9 version for WebBrowser; either IE9 is installed on system or not.
Is it possible that using IE9 with WebBrower and it may be that IE9 is not installed in my system?
With Windows Internet Explorer 8 or later the FEATURE_BROWSER_EMULATION feature defines the default emulation mode for Internet Explorer. Value 9999 - forces webpages to be displayed in IE9 Standards mode, regardless of the !DOCTYPE directive. You need IE9 or later installed on the target system. Check Internet Feature Controls (B..C)
private static void WebBrowserVersionEmulation()
{
const string BROWSER_EMULATION_KEY =
#"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
//
// app.exe and app.vshost.exe
String appname = Process.GetCurrentProcess().ProcessName + ".exe";
//
// Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
const int browserEmulationMode = 9999;
RegistryKey browserEmulationKey =
Registry.CurrentUser.OpenSubKey(BROWSER_EMULATION_KEY,RegistryKeyPermissionCheck.ReadWriteSubTree) ??
Registry.CurrentUser.CreateSubKey(BROWSER_EMULATION_KEY);
if (browserEmulationKey != null)
{
browserEmulationKey.SetValue(appname, browserEmulationMode, RegistryValueKind.DWord);
browserEmulationKey.Close();
}
}
Insert
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE="\9\" >"
Into your html page,but you have to know Web_browser control dependent on version of IE that already installed on target OS
For this, you have to lookup
What is the version of the underlying browser (registry key may return former installed version). Simplest code I use is asking the WebBrowser control
WebBrowser browser= new WebBrowser
Version ver= browser.Version(); // With ver.Major you can decide the EMULATION
The app-exe-name of your app (differs, while running in vs debug environment to "myapp".vshost.exe). This code I found somewhere:
// This code detects the .vshost. when running in vs ide
[DllImport("kernel32.dll", SetLastError=true)]
private static extern int GetModuleFileName([In]IntPtr hModule,
[Out]StringBuilder lpFilename,
[In][MarshalAs(UnmanagedType.U4)] int nSize);
public static String getAppExeName()
{
StringBuilder appname= new StringBuilder(1024);
GetModuleFileName(IntPtr.Zero, appname, appname.Capacity);
return Path.GetFileName(appname.ToString()); // return filename part
}
Now, you can calculate the Registry-Entry which is necessary for the browser compatibility. The Entry may be in Registry.LocalMachine (access rights required) or Registry.CurrentUser. I check the registry on every program start, so, I first test the existence of the entry
string regSubKey= #"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
string version= "" + ver.Version + "0000"; // installed version x 10000
string appname= getAppExeName();
RegistryKey rs = Registry.CurrentUser.OpenSubKey(regSubKey);
keyval = rs.GetValue(appname);
rs.Close();
if (keyval != null && keyval.ToString().Equals(version))
return; // already done and no browser update installed.
//
// Create key for this app and this version
rs = Registry.LocalMachine.CreateSubKey(regSubKey);
rs.SetValue(app, sversion, RegistryValueKind.DWord);
rs.Flush();
rs.Close();
In 64bit + 32bit modes, may be you have to create an entry in "Software\Wow6432Node" too
After setting the registry key, the WebBrowser control should start with the required emulation
No, the webbrowser-element (I think you mean this) is on base of IE6. You only can start the process of IE9 (don't know the name but for firefox its simply "firefox.exe") form the program.
You can read the version from the registry:
var ieVersion = Registry.LocalMachine.OpenSubKey(#"Software\Microsoft\Internet Explorer").GetValue("Version");
or
If you have a WebBrowser control you can grab it from there:
WebBrowser browser = new WebBrowser();
Version ver = browser.Version;

Webbrowser control ignores FEATURE_BROWSER_EMULATION reg entry

Im developing a custom browser solution with .net's Webbrowser control.
To disable the IE-Compatibilty View, I set the registry entry
Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION:
[Sreenshot regedit] http://zbirk.mirk.at/browserreg.png "Screenshot"
i tried to use the values: dword=8000,dword=8888,dword=9000, but the webbrowser control seems to ignore these reg entries.
Maybe someone had this problems too and may help me.
The WebBrowser control definately DOES respect these keys.
Remember that while taskman may show application.exe in the name column, if you are debugging the exe name is application.vshost.exe
So in my application sI just attempt to create the key every time the app runs. If it fails to create it (because it already exists) then I continue running, if it creates the key then I inform the user that they need to restart the application.
ensure that you are not running within vshost
the app name would be different ie appname.vshost.exe
Thx for your reply, now its working.
Her is my working peace of code:
public void setIEcomp()
{
String appname = Process.GetCurrentProcess().ProcessName+".exe";
RegistryKey RK8 = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION",RegistryKeyPermissionCheck.ReadWriteSubTree);
int value9 = 9999;
int value8 = 8888;
Version ver = webBrowser1.Version;
int value = value9;
try
{
string[] parts = ver.ToString().Split('.');
int vn = 0;
int.TryParse(parts[0], out vn);
if (vn != 0)
{
if (vn == 9)
value = value9;
else
value = value8;
}
}
catch
{
value = value9;
}
//Setting the key in LocalMachine
if (RK8 != null)
{
try
{
RK8.SetValue(appname, value, RegistryValueKind.DWord);
RK8.Close();
}
catch(Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
}
I too could not see that FEATURE_BROWSER_EMULATION made any difference in my application.
I was testing the FEATURE_BROWSER_EMULATION functionality by manually editing the registry with regedit. Nothing I did made any difference. My hosted page was still failing on any new-ish JavaScript and could not load external libraries.
I found my mistake:
I was editing the 64-bit view of the registry with regedit. My app was running as a 32-bit app and looking at the 32-bit view of the registry. That's why my changes to the registry seemed to have no impact on my application. By the way, the WPF project template defaults to "Prefer 32-bit."
Manually editing with regedit within the Wow6432Node key worked:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
Of course, setting the DWORD value programmatically within your application will also work, since your 32-bit application will edit within the Wow6432Node.
An older post and solution is no longer accurate.
Running procmon and watching for FEATURE_BROWSER_EMULATION shows the following registry variables actually checked. This was for WINWORD.exe but other than that - take your pick...
HKU\S-1-5-21-[my-sid-paws-off]\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\WINWORD.EXE
HKU\S-1-5-21-[my-sid-paws-off]\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION*
HKLM\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION(Default)
HKLM\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\WINWORD.EXE
HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\WINWORD.EXE
HKLM\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION*
HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION*

How to launch a Google Chrome Tab with specific URL using C#

Is there a way I can launch a tab (not a new Window) in Google Chrome with a specific URL loaded into it from a custom app? My application is coded in C# (.NET 4 Full).
I'm performing some actions via SOAP from C# and once successfully completed, I want the user to be presented with the end results via the browser.
This whole setup is for our internal network and not for public consumption - hence, I can afford to target a specific browser only. I am targetting Chrome only, for various reasons.
As a simplification to chrfin's response, since Chrome should be on the run path if installed, you could just call:
Process.Start("chrome.exe", "http://www.YourUrl.com");
This seem to work as expected for me, opening a new tab if Chrome is already open.
// open in default browser
Process.Start("http://www.stackoverflow.net");
// open in Internet Explorer
Process.Start("iexplore", #"http://www.stackoverflow.net/");
// open in Firefox
Process.Start("firefox", #"http://www.stackoverflow.net/");
// open in Google Chrome
Process.Start("chrome", #"http://www.stackoverflow.net/");
For .Net core 3.0 I had to use
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = #"http://www.stackoverflow.net/";
process.Start();
UPDATE: Please see Dylan's or d.c's anwer for a little easier (and more stable) solution, which does not rely on Chrome beeing installed in LocalAppData!
Even if I agree with Daniel Hilgarth to open a new tab in chrome you just need to execute chrome.exe with your URL as the argument:
Process.Start(#"%AppData%\..\Local\Google\Chrome\Application\chrome.exe",
"http:\\www.YourUrl.com");
If the user doesn't have Chrome, it will throw an exception like this:
//chrome.exe http://xxx.xxx.xxx --incognito
//chrome.exe http://xxx.xxx.xxx -incognito
//chrome.exe --incognito http://xxx.xxx.xxx
//chrome.exe -incognito http://xxx.xxx.xxx
private static void Chrome(string link)
{
string url = "";
if (!string.IsNullOrEmpty(link)) //if empty just run the browser
{
if (link.Contains('.')) //check if it's an url or a google search
{
url = link;
}
else
{
url = "https://www.google.com/search?q=" + link.Replace(" ", "+");
}
}
try
{
Process.Start("chrome.exe", url + " --incognito");
}
catch (System.ComponentModel.Win32Exception e)
{
MessageBox.Show("Unable to find Google Chrome...",
"chrome.exe not found!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Categories

Resources