Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将mshtml.IHTMLImgElement强制转换为mshtml.ihtmlelementtrender失败,没有接口_C#_.net_Internet Explorer_Com Interop_Mshtml - Fatal编程技术网

C# 将mshtml.IHTMLImgElement强制转换为mshtml.ihtmlelementtrender失败,没有接口

C# 将mshtml.IHTMLImgElement强制转换为mshtml.ihtmlelementtrender失败,没有接口,c#,.net,internet-explorer,com-interop,mshtml,C#,.net,Internet Explorer,Com Interop,Mshtml,我有一个C#.NET 4 WinForms应用程序(使用MSHTML7),可以启动新的并连接到现有的IE 10实例。它遍历所有图像并下载它们以进行操作。这种方法既耗时又冗余,因为IE已经下载了图像 我到处搜索,只有少数论坛讨论这个主题,但是所有人都能够将MSHML.IHTMLIMGEngEnter对象转换成MSHML.IHTMLeNeMeNeNeNeDER(虽然是C++代码)。 当然,我们的目标是获取完整的图像,因此也欢迎其他方法。下面是导致上述异常的代码 public static void

我有一个C#.NET 4 WinForms应用程序(使用MSHTML7),可以启动新的并连接到现有的IE 10实例。它遍历所有图像并下载它们以进行操作。这种方法既耗时又冗余,因为IE已经下载了图像

我到处搜索,只有少数论坛讨论这个主题,但是所有人都能够将MSHML.IHTMLIMGEngEnter对象转换成MSHML.IHTMLeNeMeNeNeNeDER(虽然是C++代码)。 当然,我们的目标是获取完整的图像,因此也欢迎其他方法。下面是导致上述异常的代码

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
}
我通过的一些链接无效:


您可以很容易地看到Regedit.exe问题的根源。为了让接口在进程边界之间架起大峡谷的桥梁,它需要实现接口代理和存根的代码。知道如何将接口方法参数序列化为可以从一个进程传输到另一个进程的RPC数据包的代码

COM通过查看注册表来发现该代码。您也可以使用Regedit.exe,导航到HKCR\Interfaces并向下滚动以匹配guid。您将在那里看到许多看起来像{305xxx-98B5-11CF-BB82-00AA00BDCE0B}的{guid}。是的,微软在他们的guid上作弊,更容易调试,IE有相当多的guid。它们指向标准封送拆收器,并包含描述接口的类型库guid

但是您将找到{3050F669-98B5-11CF-BB82-00AA00BDCE0B}。这基本上就是异常消息告诉您的,它找不到封送接口指针的方法。神秘的E_NOINTERFACE错误代码是回退方法也不起作用,无法查询IMarshal的结果

这在“他们忘记了”和“他们无法让它工作”之间有点纠结。这一个严重倾向于“太难使它工作”。序列化渲染接口太难了,对视频硬件的依赖太多了,当然这在一台机器和另一台机器之间永远不匹配。甚至在一台机器上,在进行任何方法调用之前,需要正确初始化图形管道是非常棘手的


责任就在这里,你不能让这一切顺利。您需要自己渲染该图像,这并不容易。对不起,请不要向信使开枪。

废话。另一种选择是使用IViewObject,它有自己的怪癖。由于您提到的实现原因,这不能通过COM互操作实现,但可以通过本机代码实现,我的理解是否正确?如果是这样的话,一个快速的本地DLL来呈现和保存图像会比IViewObject更好。我肯定会把我的答案限制在原来的问题上,并解释为什么这会出错。你要说的只是一句“祝你好运!”的评论。我将把它当作是肯定的,并给它一个机会:P。感谢你的精彩解释,以及它将为将来的com接口相关问题节省的时间。
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
}