Access current page/dom from C#

3,187 views
Skip to first unread message

Todd White

unread,
Mar 28, 2012, 10:32:43 PM3/28/12
to cefs...@googlegroups.com
Sorry if this is a duplicate of questions that may be elsewhere.

Is there any way to access the DOM of the current page in a CefSharp.WebBrowser?

I have an app that displays images on a page. I want to add a Print button to my C# window that prints a specific image on the page when pressed. I can identify the image by class or ID.

anthony taranto

unread,
Mar 28, 2012, 10:50:02 PM3/28/12
to cefs...@googlegroups.com
The CEF API has DOM access, as defined in the cef_dom.h header:

http://code.google.com/p/chromiumembedded/source/browse/trunk/include/cef_dom.h

CefSharp does not currently wrap this API.

--Anthony

Kevin Pullin

unread,
Mar 29, 2012, 12:23:12 AM3/29/12
to cefs...@googlegroups.com
One approach that might work if you'd rather not implement the cef_dom bits, is to use the Execute/Evaluate javascript functions to do whatever magic is required.

Jack O'Connor

unread,
May 19, 2012, 8:24:05 AM5/19/12
to cefs...@googlegroups.com
This is an old thread, but I thought I might add another answer just in case someone finds it.

I agree with Kevin that getting JS to do the work for you will be way easier than figuring out a way to access the DOM directly from C# or C++, if direct access to images is even possible. You could have JS try to encode the image as a string and pass it back to C# using EvaluateScript or RegisterJsObject. Here's a discussion of what that JS might try to do to stringify the image:  http://stackoverflow.com/questions/934012/get-image-data-in-javascript 

The simplest possible thing, though, is probably to have your JS pass only the image URL back to C#, which can then re-download its own copy of the image without any marshalling voodoo. (The cef_dom APIs might even let you get at an image's src attribute directly from C++, if you can navigate the DOM to find it, though CefSharp doesn't expose this. Using JS is definitely easier.)


On Wednesday, March 28, 2012 3:32:43 PM UTC-7, Todd White wrote:

Jim Bourekas

unread,
Jul 1, 2013, 8:32:16 AM7/1/13
to cefs...@googlegroups.com
I managed to do what Jack suggested:

The id of my image inside the webview is pt1:i1. Just change this and you've got yourself an image back in C#

Object base64Captcha = web_view.EvaluateScript("var img = document.getElementById(\"pt1:i1\");var canvas = document.createElement(\"canvas\");" +
                "canvas.width = img.width; canvas.height = img.height;var ctx = canvas.getContext(\"2d\");" +
                "ctx.drawImage(img, 0, 0);var dataURL = canvas.toDataURL(\"image/png\");  dataURL.replace(/^data:image\\/(png|jpg);base64,/, \"\");");

            byte[] imageBytes = Convert.FromBase64String(base64Captcha.ToString());
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);

Hope that's helpful to someone...

jim
Reply all
Reply to author
Forward
0 new messages