基本信息
源码名称:C# 从内存中获取webbrowser控件截图
源码大小:0.19M
文件格式:.zip
开发语言:C#
更新时间:2018-07-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

C# 从内存中获取webbrowser控件截图,可以后台或者隐藏窗体截图



截图后的结果如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if(RichTextBox1.Text!="")
                WebBrowser1.Navigate(RichTextBox1.Text);
        }
        internal static class NativeMethods
        {
            [StructLayout(LayoutKind.Sequential)]
            public sealed class tagDVTARGETDEVICE
            {
                [MarshalAs(UnmanagedType.U4)]
                public int tdSize;
                [MarshalAs(UnmanagedType.U2)]
                public short tdDriverNameOffset;
                [MarshalAs(UnmanagedType.U2)]
                public short tdDeviceNameOffset;
                [MarshalAs(UnmanagedType.U2)]
                public short tdPortNameOffset;
                [MarshalAs(UnmanagedType.U2)]
                public short tdExtDevmodeOffset;
            }
            [StructLayout(LayoutKind.Sequential)]
            public class COMRECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
                public COMRECT()
                {
                }
                public COMRECT(Rectangle r)
                {
                    this.left = r.X;
                    this.top = r.Y;
                    this.right = r.Right;
                    this.bottom = r.Bottom;
                }
                public COMRECT(int left, int top, int right, int bottom)
                {
                    this.left = left;
                    this.top = top;
                    this.right = right;
                    this.bottom = bottom;
                }
                public static NativeMethods.COMRECT FromXYWH(int x, int y, int width, int height)
                {
                    return new NativeMethods.COMRECT(x, y, x   width, y   height);
                }
                public override string ToString()
                {
                    return string.Concat(new object[] { "Left = ", this.left, " Top ", this.top, " Right = ", this.right, " Bottom = ", this.bottom });
                }
            }
            [StructLayout(LayoutKind.Sequential)]
            public sealed class tagLOGPALETTE
            {
                [MarshalAs(UnmanagedType.U2)]
                public short palVersion;
                [MarshalAs(UnmanagedType.U2)]
                public short palNumEntries;
            }
        }
        public Bitmap TakeSnapshot(object pUnknown, Rectangle bmpRect)
        {
            if (pUnknown == null)
                return null;
            //必须为com对象
            if (!Marshal.IsComObject(pUnknown))
                return null;
            //IViewObject 接口
            UnsafeNativeMethods.IViewObject ViewObject = null;
            IntPtr pViewObject = IntPtr.Zero;
            //内存图
            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(UnsafeNativeMethods.IViewObject)) as UnsafeNativeMethods.IViewObject;
                //调用Draw方法
                ViewObject.Draw((int)System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT, -1, IntPtr.Zero, null,
                    IntPtr.Zero, hDrawDC.GetHdc(), new NativeMethods.COMRECT(bmpRect), null, IntPtr.Zero, 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            //释放
            hDrawDC.Dispose();
            return pPicture;
        }
        [System.Security.SuppressUnmanagedCodeSecurity]
        internal static class UnsafeNativeMethods
        {
            public static Guid IID_IViewObject = new Guid("{0000010d-0000-0000-C000-000000000046}");
            [ComImport, Guid("0000010d-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IViewObject
            {
                [PreserveSig]
                int Draw([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [In] NativeMethods.COMRECT lprcBounds, [In] NativeMethods.COMRECT lprcWBounds, IntPtr pfnContinue, [In] int dwContinue);
                [PreserveSig]
                int GetColorSet([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [In] NativeMethods.tagDVTARGETDEVICE ptd, IntPtr hicTargetDev, [Out] NativeMethods.tagLOGPALETTE ppColorSet);
                [PreserveSig]
                int Freeze([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, int lindex, IntPtr pvAspect, [Out] IntPtr pdwFreeze);
                [PreserveSig]
                int Unfreeze([In, MarshalAs(UnmanagedType.U4)] int dwFreeze);
                void SetAdvise([In, MarshalAs(UnmanagedType.U4)] int aspects, [In, MarshalAs(UnmanagedType.U4)] int advf, [In, MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IAdviseSink pAdvSink);
                void GetAdvise([In, Out, MarshalAs(UnmanagedType.LPArray)] int[] paspects, [In, Out, MarshalAs(UnmanagedType.LPArray)] int[] advf, [In, Out, MarshalAs(UnmanagedType.LPArray)] System.Runtime.InteropServices.ComTypes.IAdviseSink[] pAdvSink);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap bmpScreen = TakeSnapshot(WebBrowser1.ActiveXInstance, new Rectangle(0, 0, WebBrowser1.Width, WebBrowser1.Height));
            bmpScreen.Save(Application.StartupPath   "\\"   DateTime.Now.ToString("yyyyMMddHHmmss")   ".png");  
        }
    }
}