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");
}

最新文章

  1. C# DateTime与时间戳转换
  2. CSS hack技巧大全
  3. NYOJ 737 石子合并(一)
  4. Django--models一对多
  5. Binder中的asInterface解析
  6. FastDFS之java客户端使用
  7. Migrating from IntelliJ Projects
  8. SQLSERVER存储过程语法的具体解释
  9. 基于Verilog HDL的ADC0809CCN数据采样
  10. css3 3d小demo
  11. 泛型在Web中的作用
  12. Qt: 执行cmd命令;
  13. VUE iview date-picker取时间范围...
  14. webpack+vue+vueRouter模块化构建完整项目实例详细步骤-入门篇
  15. 如何避免Android程序的ANR?
  16. [原创]Robo 3T 1.2.1 工具使用介绍
  17. 3.3 unittest批量执行
  18. Get Requests with Json Data && Get Requests with Url Parameters
  19. 【转】关于Jmeter3.0,你必须要知道的5点变化
  20. mac 终端 使用ftp命令

热门文章

  1. React 生命周期及使用场景
  2. Pytest高级进阶之Fixture
  3. MSDE2008安装备忘
  4. Java核心-多线程-并发控制器-Exchanger交换器
  5. Hbase常见错误解决方法
  6. Centos 7环境下安装配置MySQL 5.7
  7. Python中多个列表与字典的合并方法
  8. 《深度探索C++对象模型》读书笔记(二)
  9. linux下安装nexus repository及Intellij Idea集成私有maven
  10. Ubuntu下把缺省的dash shell修改为bash shell