zoukankan      html  css  js  c++  java
  • c# webBrowser 转图片

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

  • 相关阅读:
    面试题26:复杂链表的复制
    面试题25:二叉树中和为某一值的路径
    面试题24:二叉搜索树后序遍历
    面试题23:二叉树层序遍历
    面试题22:栈的压入,弹出序列
    面试题21:包含min函数的栈
    面试题20:顺时针打印矩阵
    面试题19:二叉树镜像
    plugin.go 源码阅读
    server.go 源码阅读
  • 原文地址:https://www.cnblogs.com/qiaoke/p/7859958.html
Copyright © 2011-2022 走看看