Click here to Skip to main content
15,891,864 members
Articles / Programming Languages / C#
Tip/Trick

Embedding Chrome in your C# App using CefSharp

Rate me:
Please Sign up or sign in to vote.
4.82/5 (48 votes)
2 Sep 2016CPOL3 min read 482.6K   73   57
Learn how to integrate the CefSharp open-source web browser into a C# WinForms app

Introduction

Ever tried to integrate a reliable and fast web browser in your app only to jump through hoops trying?

In this tip, you will learn how easy it is to integrate the fantastic CefSharp web browser component (based on Chromium) into your C# app.

You can then use this web browser:

  • To give users an integrated browsing experience
  • To add embedded UI programmed in HTML/JavaScript
  • For web automation

CefSharp is reliable, fast, fully open-source and does not need any extra dependencies to be installed by end users (except VC++ Runtime 2013).

In this guide, we will use a WinForms project but CefSharp works equally well with WPF projects.

Getting Started

Follow these steps to quickly get started with CefSharp.

Project

1. Create a Windows Forms Application (C#) project with .NET 4.5.2

2. Name your project (e.g. "CefTest")

Image 1

DLLs (NuGet Method)

3A) If you use NuGet, you can just grab CefSharp.WinForms from the NuGet repository, and skip the manual method below.

3B) If you used NuGet, navigate to SOLUTION > Configuration Manager and set your project to x86 or x64 since AnyCPU is not currently supported.

Image 2

DLLs (Manual Method)

PLEASE SKIP THIS SECTION IF YOU USED NUGET!

3A)  Download the CefSharp binaries and unzip them.

3B)  Right click your project & click "Add Reference", then "Browse".

Image 3

3C)  Select these DLLs:

  • CefSharp.dll
  • CefSharp.Core.dll
  • CefSharp.WinForms.dll

Image 4

3D) Select all the files in the ZIP:

Image 5

3E)  Copy those files into your Project's Debug folder (inside Bin):

Image 6

Code

4.  Right click your main form and select "View Code".

Image 7

5.  Add the following code:

Copy this into your import section:

C#
using CefSharp;
using CefSharp.WinForms;     

Copy this into your code section:

C#
public ChromiumWebBrowser browser;
public void InitBrowser(){
    Cef.Initialize(new CefSettings());
    browser = new ChromiumWebBrowser ("www.google.com");
    this.Controls.Add(browser);
    browser.Dock = DockStyle.Fill;
}

Call InitBrowser() just after the call to InitializeComponent():

C#
InitBrowser()

Your code should finally look like this:

Image 8

Run!

6.  Press F5 and you will see the Google homepage!

Image 9

Troubleshooting


An unhandled exception of type 'System.IO.FileNotFoundException' occurred in browser.exe
Additional information: Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies.

If you get this error, ensure you have Visual C++ 2013 Redistributable installed.

 


An unhandled exception of type 'System.BadImageFormatException' occurred in browser.exe
Additional information: Could not load file or assembly 'CefSharp.Core, Version=43.0.0.0, Culture=neutral, 

If you get this error, it means you are on a 64-bit PC and your app is built in AnyCPU mode. You need to change this to x86 or x64 mode:

  1. Open the Solution Explorer panel
  2. Right click your project > Project Properties
  3. Click on the Build tab
  4. Change Platform target to x86 or x64

Full Featured Browsers

If you are looking for a full-featured browser project, then check out these open source browsers built with CefSharp in C#.

WebExpress

WPF. Address Bar with Suggestions, Multiple tabs, Downloads, Bookmarks, History.

<screenshot not provided by author>

SharpBrowser

Windows Forms. Address Bar, Multiple tabs, Downloads.

Sharp Browser

History

  • V1.0 - Published the article on Nov 23rd, 2015
  • V1.1 - Added troubleshooting guide on Nov 25th, 2015
  • V1.2 - Added NuGet guide and VS project ZIP on Nov 26th, 2015
  • V1.3 - Added troubleshooting tip for 64-bit PCs on Aug 6th 2016
  • V1.4 - Updated to CefSharp v51, and .NET 4.5.2 on Aug 28th 2016
  • V1.6 - Added SharpBrowser and WebExpress projects on Sep 2th 2016

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Resizing issue Pin
Schubbard18-Sep-16 22:56
Schubbard18-Sep-16 22:56 
SuggestionI think you should use Nuget Pin
comwrg28-Aug-16 16:01
comwrg28-Aug-16 16:01 
GeneralRe: I think you should use Nuget Pin
Robin Rodricks2-Sep-16 19:55
Robin Rodricks2-Sep-16 19:55 
QuestionAfter installing not working Pin
tej narayan maurya20-Aug-16 6:34
tej narayan maurya20-Aug-16 6:34 
AnswerRe: After installing not working Pin
Robin Rodricks28-Aug-16 0:36
Robin Rodricks28-Aug-16 0:36 
QuestionCefShap Not working with version 51 Pin
Dummy Developer2-Aug-16 22:03
Dummy Developer2-Aug-16 22:03 
AnswerRe: CefShap Not working with version 51 Pin
Robin Rodricks28-Aug-16 0:37
Robin Rodricks28-Aug-16 0:37 
AnswerRe: CefShap Not working with version 51 Pin
Robin Rodricks28-Aug-16 3:43
Robin Rodricks28-Aug-16 3:43 
Questionhelp! getting an error Pin
Member 1248155724-Apr-16 5:16
Member 1248155724-Apr-16 5:16 
AnswerRe: help! getting an error Pin
Dummy Developer2-Aug-16 23:19
Dummy Developer2-Aug-16 23:19 
GeneralRe: help! getting an error Pin
Robin Rodricks7-Aug-16 1:10
Robin Rodricks7-Aug-16 1:10 
QuestionThe Browser hangs Pin
smaranh17-Feb-16 7:34
smaranh17-Feb-16 7:34 
AnswerRe: The Browser hangs Pin
Robin Rodricks19-Feb-16 1:13
Robin Rodricks19-Feb-16 1:13 
PraiseYou saved me. And there is some problem. I'm testing! Pin
Yong-nam Kim4-Jan-16 20:04
Yong-nam Kim4-Jan-16 20:04 
GeneralMessage Closed Pin
17-Jan-16 2:17
Member 1089184817-Jan-16 2:17 
QuestionAdd a button to the Form Pin
furio5029-Dec-15 4:01
furio5029-Dec-15 4:01 
QuestionDownloads not working on Box Pin
Ouhlalaa11-Dec-15 0:20
Ouhlalaa11-Dec-15 0:20 
AnswerRe: Downloads not working on Box Pin
Robin Rodricks25-Dec-15 20:26
Robin Rodricks25-Dec-15 20:26 
QuestionSection 3A downloads are not working Pin
_Jawad_8-Dec-15 9:25
professional_Jawad_8-Dec-15 9:25 
AnswerRe: Section 3A downloads are not working Pin
Robin Rodricks25-Dec-15 20:26
Robin Rodricks25-Dec-15 20:26 
PraiseFantastic - you have solved my problem! Pin
PaperTape7-Dec-15 2:04
professionalPaperTape7-Dec-15 2:04 
QuestionAre we children? :) Pin
Thornik28-Nov-15 12:53
Thornik28-Nov-15 12:53 
AnswerRe: Are we children? :) Pin
Robin Rodricks28-Nov-15 20:08
Robin Rodricks28-Nov-15 20:08 
QuestionHave you consider to post this as a tip? Pin
Nelek25-Nov-15 3:52
protectorNelek25-Nov-15 3:52 
QuestionNuget Pin
Alessandro Cavalieri24-Nov-15 9:53
Alessandro Cavalieri24-Nov-15 9:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.