您的位置:首页 > 移动开发 > Objective-C

通过IViewObject接口,取浏览器的图象,实现SNAP

2006-12-25 17:42 483 查看
今天又见到snap实现的文章,看来对此感兴趣的人挺多的.实现这个功能确实很'眩',我也来做一个把玩一下.

我的做法不是 Control.DrawToBitmap ,而是直接QueryInterface 浏览器Com对象的 IViewObject 接口,用它实现的Draw方法,画到图象上.

首先定义IViewObject的接口声名,如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Security;

using System.Runtime.InteropServices;

using System.Runtime.InteropServices.ComTypes;

namespace SnapLibrary


该接口.net 自己带了,只是internal形式,所以只有想办法用Reflector 将它弄出来,相关的还有几个类,分别是tagLOGPALETTE,COMRECT,tagDVTARGETDEVICE.

定义如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using System.Drawing;

namespace SnapLibrary


现在可以通过 Marshal.QueryInterface 将浏览器COM实例的IViewObject接口取出:

//获取接口

object hret = Marshal.QueryInterface(Marshal.GetIUnknownForObject(pUnknown),ref UnsafeNativeMethods.IID_IViewObject, out pViewObject);

pUnknown为 com对象实例.

将IViewObject 指针对象 pViewObject 转化为接口对象.

ViewObject = Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(SnapLibrary.UnsafeNativeMethods.IViewObject)) as SnapLibrary.UnsafeNativeMethods.IViewObject;


调用draw方法,绘制到图象上,以下是TakeSnapshot方法的完整代码:

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using System.Runtime.InteropServices.ComTypes;

using System.Drawing;

using System.Windows.Forms;

namespace SnapLibrary


到此既完成了对Com对象的图象抓取.那么现在给它提供一个浏览器的实例,让它实现对 web page 的快照吧.

.net 2.0提供了webbrowser对象,它是对activex对象的包装,它的使用很简单,这里就不详细说明.

WebBrowser 对象的实例的属性ActiveXInstance就是它的原生COM对象,获取它的IVewObject接口,即可调用它实现的Draw方法把网页绘制到指定的DC上.

以下是对webbrowser对象的包装类,结合Snapshot 类的类代码:

public class WebPageSnapshot : IDisposable

class Program

try

catch (Exception ex)

{

Console.WriteLine(ex.Message);

throw ex;

}


红色加粗位置.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐