我有推出新并且连接到现有的IE 10实例C#的.NET 4个WinForms应用程序(使用MSHTML 7)。 它通过所有的图片和下载他们操纵迭代。 因为图像已经被IE下载这种方法费时和冗余。
我已经找遍只有论坛的讨论屈指可数的主题,但都是能够施展mshtml.IHTMLImgElement对象mshtml.IHTMLElementRender(虽然在C ++代码)。
Unable to cast COM object of type 'mshtml.HTMLImgClass' to interface type 'mshtml.IHTMLElementRender'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F669-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
课程的目的是为了获取完整图像,从而替代方法亦欢迎。 这是导致上述异常的代码。
public static void Main (string [] args)
{
mshtml.HTMLDocument document = null;
SHDocVw.InternetExplorer explorer = null;
System.IntPtr hdc = System.IntPtr.Zero;
mshtml.IHTMLElementRender render = null;
mshtml._RemotableHandle handle = default(mshtml._RemotableHandle);
try
{
explorer = new SHDocVw.InternetExplorer();
explorer.Visible = true;
try
{
explorer.Navigate("http://www.google.com/ncr");
while (explorer.Busy)
{
// Striped events for SO example.
System.Threading.Thread.Sleep(100);
}
document = (mshtml.HTMLDocument) explorer.Document;
foreach (mshtml.IHTMLImgElement image in document.images)
{
Console.WriteLine();
if ((image.width > 0) && (image.height > 0))
{
// The following [if] will return false if uncommented.
//if (image.GetType().GetInterfaces().ToList().Contains(typeof(mshtml.IHTMLElementRender)))
{
using (Bitmap bitmap = new Bitmap(image.width, image.height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
hdc = graphics.GetHdc();
handle.fContext = hdc.ToInt32();
render = (mshtml.IHTMLElementRender) image; // Causes the exception.
//handle = (mshtml._RemotableHandle) Marshal.PtrToStructure(hdc, typeof(mshtml._RemotableHandle));
render.DrawToDC(ref handle);
graphics.ReleaseHdc(hdc);
// Process image here.
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
}
}
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Stack Trace: " + e.StackTrace);
}
explorer.Quit();
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Stack Trace: " + e.StackTrace);
}
finally
{
}
#if (DEBUG)
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadKey();
#endif
}
有些我曾经走过没有无济于事的链接:
- 捕获的HTML文档作为图像
- 无法转换类型“...”的接口类型COM对象“...”,而使用ExeCOMServer