110,539
社区成员
发帖
与我相关
我的任务
分享
HtmlDocument doc = this.CurrentMyBroswer.Document;
mshtml.HTMLDocument html = (HTMLDocument)this.CurrentMyBroswer.Document.DomDocument;
HtmlElement im = MyWebBrowser.GetElementByOuthtml(doc, "img", "logo.png");
IHTMLControlElement img_2 = im.DomElement as IHTMLControlElement;
IHTMLControlRange range = (IHTMLControlRange)((HTMLBody)html.body).createControlRange();
range.add(img_2);
range.execCommand("Copy", false, null);
range = null;
html = null;
if (Clipboard.ContainsImage())
{
using (Image img1 = Clipboard.GetImage())
{
string imgfile = string.Format(@"{0}\{1}.png", Application.StartupPath, DateTime.Now.ToString("yyyyMMddHHmmssfff"));
img1.Save(imgfile);//保存图片到磁盘
ShowMessage("img ok");
}
}
else
{
// 获取失败,
ShowMessage("img err");
return;
}
class NativeMethods
{
[ComImport]
[Guid("0000010D-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IViewObject
{
void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue);
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static void GetImage(object obj, Image destination, Color backgroundColor)
{
using (Graphics graphics = Graphics.FromImage(destination))
{
IntPtr deviceContextHandle = IntPtr.Zero;
RECT rectangle = new RECT();
rectangle.Right = destination.Width;
rectangle.Bottom = destination.Height;
graphics.Clear(backgroundColor);
try
{
deviceContextHandle = graphics.GetHdc();
IViewObject viewObject = obj as IViewObject;
viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0);
}
finally
{
if (deviceContextHandle != IntPtr.Zero)
{
graphics.ReleaseHdc(deviceContextHandle);
}
}
}
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Bitmap screenshot = new Bitmap(webBrowser1.Width, webBrowser1.Height);
NativeMethods.GetImage(webBrowser1.ActiveXInstance, screenshot, Color.White);
// 保存图片
screenshot.Save(@"F:\h11.png");
}