C#實(shí)現(xiàn)將網(wǎng)頁保存成圖片的網(wǎng)頁拍照功能
本文實(shí)例主要實(shí)現(xiàn)了網(wǎng)頁照相機(jī)程序的功能。C#實(shí)現(xiàn)將網(wǎng)頁保存成圖片格式,簡單實(shí)現(xiàn)網(wǎng)頁拍照,主要是基于ActiveX 組件的網(wǎng)頁快照類,AcitveX 必須實(shí)現(xiàn) IViewObject 接口。因此讀者完全可擴(kuò)展此類將其用于你的C#軟件項(xiàng)目中。在此特別感謝作者:隨飛提供的代碼。
主要功能代碼如下:
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 組件快照類,用于網(wǎng)頁拍照,將網(wǎng)頁保存成圖片 /// AcitveX 必須實(shí)現(xiàn) IViewObject 接口 /// 作者:隨飛 /// </summary> public class Snapshot { /// <summary> /// 取快照 /// </summary> /// <param name="pUnknown">Com 對(duì)象</param> /// <param name="bmpRect">圖象大小</param> /// <returns></returns> public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect) { if (pUnknown == null) return null; //必須為com對(duì)象 if (!Marshal.IsComObject(pUnknown)) return null; //IViewObject 接口 SnapLibrary.UnsafeNativeMethods.IViewObject ViewObject = null; IntPtr pViewObject = IntPtr.Zero; //內(nèi)存圖 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; //調(diào)用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; } } }
相關(guān)文章
c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的區(qū)別詳解
這篇文章主要介紹了c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的區(qū)別,需要的朋友可以參考下2014-07-07C#自定義鼠標(biāo)拖拽Drag&Drop效果之基本原理及基本實(shí)現(xiàn)代碼
拖拽效果無論是在系統(tǒng)上、應(yīng)用上、還是在網(wǎng)頁上,拖拽隨處可見,下面通過本文介紹下C#自定義鼠標(biāo)拖拽Drag&Drop效果之基本原理及基本實(shí)現(xiàn)代碼,需要的朋友可以參考下2022-04-04C#難點(diǎn)逐個(gè)擊破(2):out返回參數(shù)
之前提到ref是將原方法中的參數(shù)影響的結(jié)果返回到調(diào)用它的方法中,out與ref類似,相比之下,ref傳遞參數(shù)的地址,out是返回值。2010-02-02C#中倒計(jì)時(shí)功能的優(yōu)化方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了當(dāng)C#重復(fù)使用一段代碼倒計(jì)時(shí)時(shí),如何使用普通類和靜態(tài)方法,實(shí)現(xiàn)簡單的代碼封裝性、可擴(kuò)展性、可維護(hù)性,感興趣的可以了解下2024-01-01C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中
這篇文章主要介紹了C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中,是遞歸算法的一個(gè)簡單應(yīng)用,需要的朋友可以參考下2014-10-10