我正在尝试在C#中构建一个IE BHO来获取IE浏览器中加载的网页的快照。这是我正在尝试做的事情:
public class ShowToolbarBHO : BandObjectLib.IObjectWithSite
{
IWebBrowser2 webBrowser = null;
public void SetSite (Object site)
{
.......
if (site != null)
{
......
webBrowser = (IWebBrowser2)site;
......
}
}
}
另外,我p /调用以下COM方法:
[Guid("0000010D-0000-0000-C000-000000000046")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[ComImportAttribute()]
public interface IViewObject
{
void Draw([MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.LPStruct)] ref COMRECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, int dwContinue);
int GetColorSet([MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hicTargetDev, [Out] IntPtr ppColorSet);
int Freeze([MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, out IntPtr pdwFreeze);
int Unfreeze([MarshalAs(UnmanagedType.U4)] int dwFreeze);
int SetAdvise([MarshalAs(UnmanagedType.U4)] int aspects, [MarshalAs(UnmanagedType.U4)] int advf, [MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink);
void GetAdvise([MarshalAs(UnmanagedType.LPArray)] out int[] paspects, [MarshalAs(UnmanagedType.LPArray)] out int[] advf, [MarshalAs(UnmanagedType.LPArray)] out IAdviseSink[] pAdvSink);
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class COMRECT
{
public int left;
public int top;
public int right;
public int bottom;
public COMRECT()
{
}
public COMRECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisibleAttribute(true)]
[GuidAttribute("0000010F-0000-0000-C000-000000000046")]
[ComImportAttribute()]
public interface IAdviseSink
{
void OnDataChange([In]IntPtr pFormatetc, [In]IntPtr pStgmed);
void OnViewChange([MarshalAs(UnmanagedType.U4)] int dwAspect, [MarshalAs(UnmanagedType.I4)] int lindex);
void OnRename([MarshalAs(UnmanagedType.Interface)] object pmk);
void OnSave();
void OnClose();
}
现在当我拍摄快照时:
我打电话给CaptureWebScreenImage((IHTMLDocument2)webBrowser.document);
public static Image CaptureWebScreenImage(IHTMLDocument2 myDoc){
int heightsize = (int)getDocumentAttribute(myDoc, "scrollHeight");
int widthsize = (int)getDocumentAttribute(myDoc, "scrollWidth");
Bitmap finalImage = new Bitmap(widthsize, heightsize);
Graphics gFinal = Graphics.FromImage(finalImage);
COMRECT rect = new COMRECT();
rect.left = 0;
rect.top = 0;
rect.right = widthsize;
rect.bottom = heightsize;
IntPtr hDC = gFinal.GetHdc();
IViewObject vO = myDoc as IViewObject;
vO.Draw(1, -1, (IntPtr)0, (IntPtr)0, (IntPtr)0, (IntPtr)hDC, ref rect, (IntPtr)0, (IntPtr)0, 0);
gFinal.ReleaseHdc();
gFinal.Dispose();
return finalImage;
}
我没有收到网页的图片。相反,我得到一个黑色背景的图像。我不确定这是否是正确的方法,但我在网上发现IViewObject :: Draw方法用于在IE中获取网页的图像。
我之前使用Native PrintWindow()方法进行图像捕获,如以下codeproject页面中所述 - http://www.codeproject.com/KB/graphics/IECapture.aspx
但图像尺寸很大!我试图通过使用其他技术来减少尺寸。如果有人能够在上面的代码中指出错误(我相信会有很多错误)会很棒。
谢谢, 卡皮尔
答案 0 :(得分:2)
您可以尝试查询IHTMLElementRender接口以查找文档元素(可通过IHTMLDocument3接口获得)并使用DrawToDC方法将页面呈现为位图。
答案 1 :(得分:0)
1 IHTMLElementRender :: DrawToDC已被弃用。就我所见,它不会像闪光一样画出物体和画布。
最好调用IViewObject :: Draw()或:: OleDraw()。
2我不知道怎么用C#做,但我知道如何在win32程序中做到这一点。在win32程序中,您需要调用IOleInplaceObject :: SetObjectRect来设置IWebBrowser的位置和大小。 或者SetClientSite(),我相信他们中的一个能够做到这一点。
CComQIPtr oleObject = browser;
CComQIPtr<IOleInPlaceObject> inPlaceObject(mOleObject);
inPlaceObject->SetObjectRects(&rcClient, &rcClient);
oleObject ->SetClientSite(clientSite);
希望有所帮助