Use Chromium as Web Browser in Visual Studio
Internet Explorer is such a buggy software and it is even worse when you are using it as a browser for your Visual Studio / .NET program. It is not a big surprise why third party software was used widely naming Awesomium as one of the leading third party provider. It is mainly used in providing HTML based UI for inline game stores.
Problems with the Default IE WebBrowser
The default webbrowser class is an emulation wrapper of IE 6 / IE 7 on top of the existing installed Internet Explorer. This pose a lot of problems due to inherent bugs and incomplete functionality of Internet Explorer.
There is no easy way to use the current installed browser other than hacking the registry which require an administrator access.
Webpages won’t render perfectly due to missing HTML 5 handling and proper CSS handling.
There are more issues related to how Microsoft have implemented the Web Browser class and one of the main trouble is generating a screenshot of webpages. For a developer and a data miner, this pose as a huge brick wall or a dead end.
What is Chromium
Chromium is the open-source web browser project from which Google Chrome draws its source code. The browsers share the majority of code and features, though there are some minor differences in features and they have different licensing.
In order to utilize Chromium as our default Web browser requires extensive programming to import the Dlls from the chromium project and embed it into our program. On the good side if you won’t bother loosing the capability to directly communicate with Chromium, you are in luck.
A github project called CefSharp will make the job easier and it is available in 3 library versions.
- Windows Forms
- Windows Presentation Foundation (WPF)
- Offscreen (No Visual Display)
Downloading CefSharp
CefSharp can be easily download using Visual Studios Package Manager. This is my favorite tool since it acts like CentOS’s yum module.
Installing CefSharp Library for WPF library using Install-Package CefSharp.Wpf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
PM> Install-Package CefSharp.Wpf Attempting to gather dependencies information for package 'CefSharp.Wpf.45.0.0' with respect to project 'RImage', targeting '.NETFramework,Version=v4.5.2' Attempting to resolve dependencies for package 'CefSharp.Wpf.45.0.0' with DependencyBehavior 'Lowest' Resolving actions to install package 'CefSharp.Wpf.45.0.0' Resolved actions to install package 'CefSharp.Wpf.45.0.0' Adding package 'cef.redist.x64.3.2454.1344' to folder 'C:\Source\RImage\packages' Added package 'cef.redist.x64.3.2454.1344' to folder 'C:\Source\RImage\packages' Added package 'cef.redist.x64.3.2454.1344' to 'packages.config' Successfully installed 'cef.redist.x64 3.2454.1344' to RImage Adding package 'cef.redist.x86.3.2454.1344' to folder 'C:\Source\RImage\packages' Added package 'cef.redist.x86.3.2454.1344' to folder 'C:\Source\RImage\packages' Added package 'cef.redist.x86.3.2454.1344' to 'packages.config' Successfully installed 'cef.redist.x86 3.2454.1344' to RImage Adding package 'CefSharp.Common.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.Common.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.Common.45.0.0' to 'packages.config' Successfully installed 'CefSharp.Common 45.0.0' to RImage Adding package 'CefSharp.Wpf.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.Wpf.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.Wpf.45.0.0' to 'packages.config' Successfully installed 'CefSharp.Wpf 45.0.0' to RImage |
Installing CefSharp Library for WPF library using Install-Package CefSharp.Winforms
1 2 3 4 5 6 7 8 9 10 |
PM> Install-Package CefSharp.winforms Attempting to gather dependencies information for package 'CefSharp.winforms.45.0.0' with respect to project 'RImage', targeting '.NETFramework,Version=v4.5.2' Attempting to resolve dependencies for package 'CefSharp.winforms.45.0.0' with DependencyBehavior 'Lowest' Resolving actions to install package 'CefSharp.winforms.45.0.0' Resolved actions to install package 'CefSharp.winforms.45.0.0' Adding package 'CefSharp.WinForms.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.WinForms.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.WinForms.45.0.0' to 'packages.config' Successfully installed 'CefSharp.WinForms 45.0.0' to RImage |
Finally, installing CefSharp Library for WPF library using Install-Package CefSharp.Offscreen
1 2 3 4 5 6 7 8 9 |
PM> Install-Package CefSharp.offscreen Attempting to gather dependencies information for package 'CefSharp.offscreen.45.0.0' with respect to project 'RImage', targeting '.NETFramework,Version=v4.5.2' Attempting to resolve dependencies for package 'CefSharp.offscreen.45.0.0' with DependencyBehavior 'Lowest' Resolving actions to install package 'CefSharp.offscreen.45.0.0' Resolved actions to install package 'CefSharp.offscreen.45.0.0' Adding package 'CefSharp.OffScreen.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.OffScreen.45.0.0' to folder 'C:\Source\RImage\packages' Added package 'CefSharp.OffScreen.45.0.0' to 'packages.config' Successfully installed 'CefSharp.OffScreen 45.0.0' to RImage |
Using Chromium
There is a Minor Glitch (as noticed on my machine) in loading the libraries. After downloading and automatically importing the libraries, your project file will refuse to refresh. In order to fix reference issues, you should close the solution/project and reload.
Chromium currently have no easy way of adding the browser inside your toolbox so we are going to add it the old way in the cs source file.
1 |
ChromiumWebBrowser cbrowser = new ChromiumWebBrowser("coderinthebox.com"); |
And add it to our winform program using controls.add().
1 2 3 4 5 |
// PanelContent1 is my existing panel panelContent1.Controls.Add(cbrowser); // Make sure that you dock the browser to occupy all available space cbrowser.Dock = DockStyle.Fill; |
Chromium Inside a Windows Form
Enjoy!