Ryan Farber's WebLog

DrawToDC

I’m trying to take this code http://www.codeproject.com/internet/htmlimagecapture.asp?msg=783507 and convert it to C#.

I can’t seem cast from IHTMLElement down to IHTMLElementRender.  Trying through late binding also fails.

IHTMLDocument2 doc = (IHTMLDocument2)Browser.Document;
IHTMLElement body = (IHTMLElement)doc.body;

// Render returns as null
IHTMLElementRender render = (IHTMLElementRender)body;

// Render is also null
IHTMLElementRender render = body.getType().GetInterface(“IHTMLElementRender”);

Does anyone have working C# calling DrawToDC?

Published Tuesday, October 12, 2004 9:06 AM by rfarber

Comments

 

Geoff Appleby said:

Without actually going and trying it myself, i would hazard a guess that the IHTMLElementRender interface isn't an actual inherited interface of the body element.

QueryInterface somtimes does more than just cast the object to a different interface, I'd make a bet that the QueryInterface for IHtmlElementRender is acting like a factory, and returning a new object. So therefore, when you just try and cast to that render interface, you get back null.

Try calling QueryInterface directly on your body element, and see what that gives you. Let me know how you :)

--Geoff
October 12, 2004 11:10 AM
 

Geoff Appleby said:

how you go, i mean to say.
October 12, 2004 11:11 AM
 

Darron said:

I tried this real quick and it works. I threw together a form with an IE control and a panel. There is an issue with import of the MSHTML interface so just redeclare it locally and you will have no problems. If render is null, you may have a frame to deal with.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using SHDocVw;
using mshtml;

namespace ScreenGrab
{
[
Guid("3050f669-98b5-11cf-bb82-00aa00bdce0b"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
ComVisible(true),
ComImport
]
interface IHTMLElementRender
{
void DrawToDC([In] IntPtr hDC);
void SetDocumentPrinter([In, MarshalAs(UnmanagedType.BStr)] string bstrPrinterName, [In] IntPtr hDC);
};

public class IEElementCapture
{
private IWebBrowser2 webBrowser = null;

public IEElementCapture(IWebBrowser2 webBrowser)
{
this.webBrowser = webBrowser;
}

public bool Capture(ref Graphics g)
{
if ( null == webBrowser )
{
return false;
}

IHTMLDocument2 htmlDocument = (IHTMLDocument2) webBrowser.Document;
if ( null != htmlDocument )
{
IHTMLElement bodyElement = (IHTMLElement) htmlDocument.body;
if ( null != bodyElement )
{
IHTMLElementRender render = (IHTMLElementRender) bodyElement;
if ( null != render )
{
IntPtr memDC = g.GetHdc();
render.DrawToDC(memDC);
return true;
}
}
}

return false;
}
}
}
October 12, 2004 5:05 PM
 

Ryan said:

This is perfect. Thank you. It is interesting that it doesn't pick up the custom DHTML filters like the gradients applied on www.microsoft.com home page.
October 13, 2004 2:12 AM
 

dd said:

Is there a VB.net version of this code?
November 10, 2004 4:05 AM
 

Alan Dean said:

[How To] Generate an image of a web page
April 24, 2005 11:09 PM
 

DrawToDC said:

November 26, 2007 9:56 PM
 

Actors and Actresses » Archive du blog » Ryan Farber’s WebLog : DrawToDC said:

January 4, 2008 3:09 PM
 

html to image | FreeLance Home Jobs said:

September 24, 2008 11:14 PM
 

Ryan Farber s WebLog DrawToDC | work from home said:

June 16, 2009 7:31 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker