Running Local Web Pages in CefSharp.WPF

by filip 18. January 2017 18:33

I needed to create a program which will run a web page from the local file system. Using the WebBrowser control wasn't really an option since that uses the Trident engine which didn't support many of the things we were trying to do in the web page. I went with the latest build of CefSharp.Wpf, but that doesn't natively run local files, so it had to be modified. Fortunately, I found some posts on how to enable that feature (here and here) - however, the first link was for an older version of CefSharp, and the second link only works with static resource files. I ended up merging both concepts to get it to work.

The code below works on version 53.0.1 of CefSharp.Wpf. 

First, the following two classes needed to be created:


using System;
using CefSharp;
namespace MyCustomNamespace
    public class LocalResourceHandlerFactory : IResourceHandlerFactory
        public bool HasHandlers
                return true;

        public IResourceHandler GetResourceHandler(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request)
            return new LocalResourceHandler();


using System;
using System.IO;
using CefSharp;
using System.Collections.Generic;

namespace MyCustomNamespace
    class LocalResourceHandler : IResourceHandler
        private string mimeType;
        private MemoryStream stream;

        public void Cancel()


        public bool CanGetCookie(Cookie cookie)
            return true;

        public bool CanSetCookie(Cookie cookie)
            return true;

        public void Dispose()


        public void GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl)
            responseLength = stream == null ? 0 : stream.Length;
            redirectUrl = null;

            response.StatusCode = (int)System.Net.HttpStatusCode.OK;
            response.StatusText = "OK";
            response.MimeType = mimeType;

        public bool ProcessRequest(IRequest request, ICallback callback)
            // The 'host' portion is entirely ignored by this scheme handler.
            var uri = new Uri(request.Url);
            var file = uri.AbsolutePath;

            if (File.Exists(file))
                Byte[] bytes = File.ReadAllBytes(file);
                stream = new MemoryStream(bytes);
                switch (Path.GetExtension(file))
                    case ".html":
                    case ".htm":
                        mimeType = "text/html";
                    case ".css":
                        mimeType = "text/css";
                    case ".js":
                        mimeType = "text/javascript";
                    case ".png":
                        mimeType = "image/png";
                    case ".jpg":
                    case ".jpeg":
                        mimeType = "image/jpeg";
                    case ".gif":
                        mimeType = "image/gif";
                    case ".woff":
                        mimeType = "application/font-woff";
                    case ".eot":
                        mimeType = "application/";
                    case ".ttf":
                        mimeType = "application/font-ttf";
                    case ".otf":
                        mimeType = "application/font-otf";
                    case ".svg":
                        mimeType = "image/svg+xml";
                    case ".appcache":
                    case ".manifest":
                        mimeType = "text/cache-manifest";
                        mimeType = "application/octet-stream";
                return true;
            return false;

        public bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback)
            //Dispose the callback as it's an unmanaged resource, we don't need it in this case

            if (stream == null)
                bytesRead = 0;
                return false;

            //Data out represents an underlying buffer (typically 32kb in size).
            var buffer = new byte[dataOut.Length];
            bytesRead = stream.Read(buffer, 0, buffer.Length);

            dataOut.Write(buffer, 0, buffer.Length);

            return bytesRead > 0;

With the above code in place, we can now load the web page from a local file. The code below assumes that the HTML files are located in a 'web' subdirectory (for example, /bin/debug/web/)

public MainWindow()
            var settings = new CefSharp.CefSettings { RemoteDebuggingPort = 1234 };
            // for some reason, performance sucks w/ the gpu enabled
            settings.CefCommandLineArgs.Add("disable-gpu", "1");
            settings.CefCommandLineArgs.Add("disable-gpu-vsync", "1");


            browser.ResourceHandlerFactory = new LocalResourceHandlerFactory();

            browser.BrowserSettings.FileAccessFromFileUrls = CefSharp.CefState.Enabled;
            browser.BrowserSettings.UniversalAccessFromFileUrls = CefSharp.CefState.Enabled;
            browser.BrowserSettings.WebSecurity = CefSharp.CefState.Disabled;
            //browser.BrowserSettings.WebGl = CefSharp.CefState.Disabled;

            string dir = System.IO.Directory.GetCurrentDirectory();
            String page = string.Format("{0}/web/index.html", dir);
            browser.Address = page;


Tags: , ,

Web Development | Windows Development

Adobe Creative Cloud Subscription for Business Catalyst Website

by filip 21. June 2012 09:40

We recently got the Adobe Creative Cloud subscription at work, and I wanted to check out their web hosting plan – one of the features is hosting for up to five websites.

Step 1: Creating a Website

I’ve never hosted a website with Adobe (or, more specifically, Business Catalyst), so I wasn’t sure where to start.  I looked around the website, and eventually found that there are three ways to create a website:

  • Muse: an Adobe program for non-programmers to create websites
  • Dreamweaver: an Adobe program for programmers to create websites (last time I used this was maybe 12 years ago)
  • an online management tool for websites

The documentation I read on indicated that even if I go the Muse or Dreamweaver route, I’ll eventually need to use BusinessCatalyst to manage my site for things such as DNS, so I went to  Also, I didn’t really feel like installing Muse or Dreamweaver, as I have no intention of using either of those programs (I like to use Visual Studio for those types of tasks).

Well, turns out that creating my website with was a mistake. Creative Cloud subscriptions come with five webBasic websites. These types of websites can be created from Muse (or possibly Dreamweaver).  However, creating from will create a webCommerce site. I noticed the problem when I tried to promote my site to live. When clicking the “Upgrade” button, it started asking for my credit card info. After a chat with their CSR, my site was “downgraded” to webBasic and I was able to push the site to live.

Step 2: DNS – Creating a Subdomain

I did not want to move my website to Business Catalyst. What I wanted was a place to store files on the Adobe Cloud, while keeping my website where it was.  To do that, I wanted a subdomain,, to be hosted on Adobe’s cloud. Everything else with would remain where it was.

The first step is to go to the administration section of the website created in Step 1 above, clicking on Manage Site-> Site Settings –> Site Domains. Click on “New Domain” button on the top of the page, and configure the domain. In the “Domain” field, enter the full domain name of your site. So, in my example, I entered “”, without the quotes. Select “Use an external DNS server” radio button option, and copy the IP address listed. You will need this in the next step.  Click on the “Save” button.

Now that you have the IP address of your site, log into the DNS manager of your current website (which in my case was Arvixe, but it could just as well have been GoDaddy or something similar.  You will need to create an “A” record for your domain, and point it to the IP address you just copied from BusinessCatalyst when creating your domain. So, in my example, I created an “A” record for "” with a name of “images”, and pointed it to the correct IP address.

At this point, you should be able to go to the domain you just created. Type the full domain (in my case, into your browser and you should now be at the BusinessCatalyst website.

Step 3: FTP files to your Adobe/Business Catalyst website

You’ll need to obtain the FTP settings for your website from BusinessCatalyst. Log into the admin section of the website created in Step 1, and go to “ManageSite” –> “Site Manager” –> “File Manager”, and click on the “Upload Files” button at the top of the page. There should be a Tip on this page, and if you click on the “See Details” button, it will show you the details for your FTP login info. In my case, it was:

  • host:
  • username:{Business Catalyst username}
  • password: {Business Catalyst password}

I use FileZilla to FTP, and I entered in the information above. I was rather disappointed that BusinessCatalyst used standard FTP with plain text encryption. Doesn’t seem very secure, but it is what it is.

I was able to upload “.aspx” pages to the site, however, it did not execute. Seems like the handler for the file types is disabled. However, other files, such as images, serve up just fine. One thing to keep in mind is that if you upload HTML files, they will not be exactly the same when you access them with a browser. BusinessCatalyst adds some of their own stuff into your files.

Tags: ,

Personal | Web Development

Improving Google Page Speed Score for BlogEngine

by filip 22. February 2012 14:47

I was getting a 72/100 score from Google Page Speed, so I attempted to improve that a bit.

I added caching to static resources via the web config, which raised my score by about 10:

  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
  <remove fileExtension=".mp4" />
  <remove fileExtension=".gif" />
  <remove fileExtension=".htm" />
  <remove fileExtension=".html" />
  <remove fileExtension=".jpeg" />
  <remove fileExtension=".jpg" />
  <remove fileExtension=".js" />
  <remove fileExtension=".png" />
  <remove fileExtension=".txt" />
  <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
  <mimeMap fileExtension=".gif" mimeType="image/gif" />
  <mimeMap fileExtension=".htm" mimeType="text/html" />
  <mimeMap fileExtension=".html" mimeType="text/html" />
  <mimeMap fileExtension=".jpeg" mimeType="image/jpeg" />
  <mimeMap fileExtension=".jpg" mimeType="image/jpeg" />
  <mimeMap fileExtension=".js" mimeType="application/x-javascript" />
  <mimeMap fileExtension=".png" mimeType="image/png" />
  <mimeMap fileExtension=".txt" mimeType="text/plain" />

New page score: 82/100

I then made sure that all of my custom javascript files went through the javascript handler in BlogEngine, which raised my score to by 3:

<script language="javascript" type="text/javascript" 

New page score: 85/100

I then uninstalled the syntax highlighter extension from BE. This upped my page rank by another 3.

New page score: 88/100

There's still some room for improvement, though... I want to get as close as possible to 100 :)

Tags: , , , ,

Web Development

Internet Explorer 9 (IE9) CSS3 Support – Platform Preview

by filip 16. March 2010 13:05

I downloaded the platform preview version of IE9 today to test out some CSS support. I ran a few tests through it, with the following results.  The picture on the left was generated using Firefox 3.6 (keep in mind that I did have to use the “-moz” prefix in front of all but one style to make it work in FF). The IE content is on the right.


It certainly is possible that IE requires some vendor-specific suffix like Firefox did. However, I did not use any prefix, so the style may actually be supported in some form.

In any case, the only supported feature out of the four I tested was border-radius (and that is actually the one I’m most excited about). The box-shadow would be next on my list. Hopefully that gets put in the next preview.

Tags: , ,

Web Development

Extract Formatted Text From Excel Cell With C# (Rich Text Format)

by filip 16. September 2009 23:17

I was writing an application that needed to convert text in a cell in an Excel workbook to HTML. It is fairly trivial to get formatting for the entire cell, but each individual character in the cell could have different formatting itself, so I needed something more specific than cell-level formatting info.

At first, I started using the Excel.Range.get_Characters( pos, len ) method to get info out of the cell.  The code would loop through all characters, get them one by one, and check the formatting.  For example:

   1: Microsoft.Office.Interop.Excel.Range Range = (Microsoft.Office.Interop.Excel.Range)Cell;
   2: int TextLength = Range.Text.ToString().Length;
   3: for (int CharCount = 1; CharCount <= TextLength; CharCount++)
   4: {
   5:     Microsoft.Office.Interop.Excel.Characters charToTest = Range.get_Characters(CharCount, 1);
   6:     bool IsBold = (bool)charToTest.Font.Bold;
   7:     bool IsItalic = (bool)charToTest.Font.Italic;
   8:     // other formatting tests here
   9: }

However, that method proved to be incredibly slow for cells that have more than just a few characters.  For cells that have 1000+ characters, it would take several minutes to run the test across all characters. I kept playing around with different ways to speed up the whole process, but it just became apparent that making the call to Excel to get all of this information was not going to be acceptable.

Finally, I think I’ve found the solution. It is possible to copy the text from a cell to the clipboard, and then use the Clipboard class to retrieve the formatted text, and parse it with C#. I ended up using the System.Windows.DataFormats.Rtf format to extract the data from the clipboard in the following way:

string rtf = (string)System.Windows.Clipboard.GetData(System.Windows.DataFormats.Rtf);

Then, I create a System.Windows.Forms.RichTextBox, and use that to parse the data. The following is a sample of the solution, and it is reasonably quick.

   1: Microsoft.Office.Interop.Excel.Range Range = (Microsoft.Office.Interop.Excel.Range)Cell;
   2: Range.Copy(System.Reflection.Missing.Value);
   4: string rtf = (string)System.Windows.Clipboard.GetData(System.Windows.DataFormats.Rtf);
   5: System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox();
   6: rtb.Rtf = rtf;
   8: int CharCount = rtb.Text.Length;
  10: for (int CharNum = 0; CharNum < CharCount; CharNum++)
  11: {
  12:    rtb.Select(CharNum, 1);
  13:    System.Drawing.Font Font = rtb.SelectionFont;
  14:    bool IsCharBold = Font.Bold;
  15:    bool IsCharUnderline = Font.Underline;
  16:    bool IsCharItalic = Font.Italic;
  18:    // other code here

I was also asked about getting the color in the comments. To get the color, you can use:

System.Drawing.Color color = rtb.SelectionColor;

There are also other properties of rtb dealing with selection, such as SelectionAlignment, SelectionBackColor, etc. See the RichTextBox class for more info.

Tags: , , ,

Windows Development

Bloodforge Band

Looking for the Bloodforge band site? Click here.

About Filip Stanek

Death Note Pic I'm a developer at ACG in Cincinnati, OH. I like ASP.NET, Flash, and other web technologies, & enjoy playing chess, video games, etc.

Currently playing:


Month List