C#实现将网页保存成图片的网页拍照功能
程序员文章站
2024-02-20 13:43:16
本文实例主要实现了网页照相机程序的功能。c#实现将网页保存成图片格式,简单实现网页拍照,主要是基于activex 组件的网页快照类,acitvex 必须实现 iviewob...
本文实例主要实现了网页照相机程序的功能。c#实现将网页保存成图片格式,简单实现网页拍照,主要是基于activex 组件的网页快照类,acitvex 必须实现 iviewobject 接口。因此读者完全可扩展此类将其用于你的c#软件项目中。在此特别感谢作者:随飞提供的代码。
主要功能代码如下:
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 { /// <summary> /// activex 组件快照类,用于网页拍照,将网页保存成图片 /// acitvex 必须实现 iviewobject 接口 /// 作者:随飞 /// </summary> public class snapshot { /// <summary> /// 取快照 /// </summary> /// <param name="punknown">com 对象</param> /// <param name="bmprect">图象大小</param> /// <returns></returns> public bitmap takesnapshot(object punknown, rectangle bmprect) { if (punknown == null) return null; //必须为com对象 if (!marshal.iscomobject(punknown)) return null; //iviewobject 接口 snaplibrary.unsafenativemethods.iviewobject viewobject = null; intptr pviewobject = intptr.zero; //内存图 bitmap ppicture = new bitmap(bmprect.width, bmprect.height); graphics hdrawdc = graphics.fromimage(ppicture); //获取接口 object hret = marshal.queryinterface(marshal.getiunknownforobject(punknown), ref unsafenativemethods.iid_iviewobject, out pviewobject); try { viewobject = marshal.gettypedobjectforiunknown(pviewobject, typeof(snaplibrary.unsafenativemethods.iviewobject)) as snaplibrary.unsafenativemethods.iviewobject; //调用draw方法 viewobject.draw((int)dvaspect.dvaspect_content, -1, intptr.zero, null, intptr.zero, hdrawdc.gethdc(), new nativemethods.comrect(bmprect), null, intptr.zero, 0); marshal.release(pviewobject); } catch (exception ex) { console.writeline(ex.message); throw ex; } //释放 hdrawdc.dispose(); return ppicture; } } }