网页htmt转为图片(net2.0)

来源:互联网 发布:明朝那些事儿 知乎 编辑:程序博客网 时间:2024/05/19 17:48

第一步 using System.Runtime.InteropServices;

 第二步 public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter

        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
            int nWidth, int nHeight, IntPtr hObjectSource,
            int nXSrc, int nYSrc, int dwRop);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
            int nHeight);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

 

 第三步 using (Graphics srcGraphics = this.webBrowser1.CreateGraphics())
            {
                using (Graphics destGraphics = this.pictureBox1.CreateGraphics())
                {
                    IntPtr hdcDest = destGraphics.GetHdc();
                    IntPtr hdcSrc = srcGraphics.GetHdc();
                    BitBlt(
                         hdcDest,
                         0, 0,
                         this.webBrowser1.ClientRectangle.Width, this.webBrowser1.ClientRectangle.Height,
                         hdcSrc,
                         0, 0,
                         (int)SRCCOPY
                         );
                    srcGraphics.ReleaseHdc(hdcSrc);
                    destGraphics.ReleaseHdc(hdcDest);
                }
            } 

原创粉丝点击