How is all started
So this week I was working on an extension for WebMatrix, Luke Sampson of http://StudioStyle.es just integrate a cool piece of code from Matt MCElheny. The news is that the studiostyle.es website now supports converting the over 1,000 themes uploaded for Visual Studio 2010 into the WebMatrix format, and hence we automatically got a very large load of themes to choose from.
Still we aspired for an even better experience, currently the WebMatrix user will have to install the ColorThemeEditor extension, go to the site, find a theme, download it, import it, and so on. Just too many steps. So Luke and some other folks (thanks Justin Beckwith and Scott Hanselman) started chatting. Luke was super fast, two hours later he announced that there is an api to the site.
http://studiostyl.es/api/schemes.xml I could now get a live feed of all the themes available
and for each one he provided a preview, and a download link. So I was ready to write the update to my extension.
The preview came from the following style link http://studiostyl.es//schemes/son-of-obsidian/snippet
This returns an HTML page, that looks like this:
and I was planning to show one per item in a WPF list view. Unfortunately the web browser control is not too happy inside a WPF life (since it’s basically a thin wrapper around native window). So instead I decided to render the browser into a bitmap and then show it on the list. I Spent several hours playing around and searching for a solution online, and couldn’t find any valid solution for my case.
When I came back to work, I chatted with Mikhail Arkhipov and he has some old class that did something very similar.
What doesn’t work
The WPF WebBrowser control is really just a wrapper around the WinFrom WebBrowser control. So at first I thought I’ll load the page off the screen and render it. Well it turns out that if this control is not on the screen it will never actually render. I wasn’t inclined to add it to the view as a hack, but I tried anyways. Well turns out that rendering the image using RenderTargetBitmap doesn’t work either. I guess because it’s an activeX and not a true WPF control.
So what is the solution
First I used a WinForms WebBrowser, so instead of instantiating a System.Windows.Controls.WebBrowser I used System.Windows.Forms.WebBrowser
Things already start to look better because I got the browser to render without going on the screen. I still couldn’t figure out how to render the bitmap. This where mshtml came to help.
The browsers document can be cast into IViewObject like in the code below:
and now rendering can happen.
In my code I wasn’t hitting the server directly using Navigate and in order to do verify that I actually got the site data back rather than an error page I looked for a <pre tag that I knew Luke is always including in the response and is highly unlikely in an error page.
Of course I could have gotten an HttpWebRequest get the response and then feed the browser with the plain html, except that this page actually has some Jquery links, and it felt way more natural to let the browser deal with it. It’s really up to the implementer to pick what suits his application.
I’m attaching the full code below, here is some explanations on how it works and how to use it.
How to use it:
1. Add a reference to mshtml it’s going to appear in the COM section in VS2010
2. In your code instantiate a WebBrowserUtility utility object
3. Whenever you get a uri you want to render hookup to the WebBrowserImageReady event, and call Navigate with the Uri
4. The event will fire back after some time with the image on the eventargs, in my case if the html text did not include “<pre” it will return null.
5. In my case I use CroppedImage to remove the scrollbars and I just render larger than I need (see lines 41, 42), cheap and dirty tricks that works nicely in my case. It’s a bit more complex to remove the scrollbars, but there are plenty of blogs on how to achieve that.
A word of caution: The webbrowserutility might not call back on the event, so you might want to spin up a timer disconnect it and dispose it after a while.
That’s it you are ready to roll.
How it works:
When you instantiate the utility a WinForms browser is generated, that is important for rendering off the screen. The WPF one simply never fires an event back when it’s ready in this case.
I made navigate a separate method to avoid timing issues in hooking up the WebBrowserImageReady event. It can only be called once! The browser is disposed after the first navigation. Of course this can be modified to make the class reusable and disposable.
When the browser finishes downloading it fires document ready event, at that point I’m getting the Document.Text and doing a simple text search for a <pre (yes it could be better, in my case it’s a sufficient test).
I then create an image and “Cast” or QueryInterface really for the IViewObject. If I got it I’ll create graphics and Draw it on the bitmap (that’s the method signature defined below on line 132+).
Since this is a WPF app, I now convert it back to a WPF Image, and raise the event. If I failed in any way the event fires with a null image.
And this it how it looks at the end
Come give it a spin
-> Install webmatrix from here:
-> Open any site
-> Click on the gallery icon:
-> Install ColorThemeManager
-> Switch to to the Files workspace, open any file
-> Click on the Theme button
-> Notice the new fun colors in your editor