我有C# .NET 4 WinForms应用程序(使用MSHTML 7),它启动新的并连接到现有的IE10实例。它遍历所有图像并下载它们以进行操作。这种方法是耗时和多余的,因为图像已经下载了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
}我浏览过的一些链接没有结果:
发布于 2013-06-04 13:37:19
您可以很容易地看到Regedit.exe问题的根源。为了使接口在进程边界之间架设大峡谷的桥梁,它需要实现代理和接口存根的代码。知道如何将接口方法参数序列化为可从一个进程传输到另一个进程的RPC数据包的代码。
COM通过查看注册表来发现该代码。您也可以使用Regedit.exe,导航到HKCR\接口并向下滚动以匹配guid。你会看到很多{guid},看起来像{305 11CF 98B5-11CF-BB82-00AA00BDCE0B}。是的,微软欺骗他们的guids,更容易调试,IE有相当多的他们。它们指向标准封送处理程序,并包括描述接口的类型库guid。
但您将发现not {3050F669-98B5-11CF-B82-00AA00BDCE0B}。这基本上就是异常消息告诉您的,它找不到一种方法来封送接口指针。神秘的E_NOINTERFACE错误代码是后备方法也无法工作的结果,无法查询IMarshal。
这是在“他们忘记”和“他们无法使它发挥作用”之间的一种折腾。这一条严重倾向于“太难使它发挥作用”。序列化渲染接口太困难了,视频硬件上的依赖性太大了,当然,视频硬件在一台机器和另一台机器之间从来不匹配。甚至在一台机器上,在进行任何方法调用之前,都需要正确地初始化图形管道,这是非常棘手的。
这就是责任到此为止的地方,你不能让这件事成功。你需要自己渲染这个图像,这并不容易。对不起,请不要开枪打送信人。
https://stackoverflow.com/questions/16918299
复制相似问题