• Browser GetImage


    using Microsoft.Win32;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Console_21697048
    {
        /// <summary>
        /// http://stackoverflow.com/q/21697048/1768303
        /// </summary>
       internal class Program2
        {
            //const string HTML = "<!DOCTYPE html><body style='background-color: grey'><div style='background-color: blue; opacity: 0.2; color: yellow'>This is a text</div></body>";
            const string FILE_NAME = @"d:webpage.png";
            //readonly static Size IMAGE_SIZE = new Size(320, 200);
    
            // Main
            public static void Main2(string[] args)
            {
                try
                {
                    // enable HTML5 etc (assuming we're running IE9+)
                    SetFeatureBrowserFeature("FEATURE_BROWSER_EMULATION", 9000);
                    // force software rendering
                    SetFeatureBrowserFeature("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI", 1);
                    SetFeatureBrowserFeature("FEATURE_GPU_RENDERING", 0);
    
                    using (var apartment = new AnfleCrawler.Common.MessageLoopApartment())
                    {
                        // create WebBrowser on a seprate thread with its own message loop
                        var webBrowser = apartment.Invoke(() => new WebBrowser());
    
                        // navigate and wait for the result 
                        apartment.Invoke(() =>
                        {
                            var pageLoadedTcs = new TaskCompletionSource<bool>();
                            webBrowser.DocumentCompleted += (s, e) =>
                                pageLoadedTcs.TrySetResult(true);
    
                            //webBrowser.DocumentText = HTML;
                            webBrowser.Navigate(AnfleCrawler.UITest.IEForm.ConstUris.Taobao);
                            return pageLoadedTcs.Task;
                        }).Wait();
                        // save the picture
                        apartment.Invoke(() =>
                        {
                            webBrowser.Size = webBrowser.Document.Body.ScrollRectangle.Size;
                            webBrowser.Document.InvokeScript("eval", new object[]{@"    window.addEventListener('load', function () {
            alert('test!');
            window.focus();
            window.scrollTo(0, document.documentElement.offsetHeight);
        });"});
                            var rectangle = new Rectangle(0, 0, webBrowser.Width, webBrowser.Height);
                            System.Threading.Thread.Sleep(20000);
                            // get reference DC
                            using (var screenGraphics = webBrowser.CreateGraphics())
                            {
                                var screenHdc = screenGraphics.GetHdc();
                                // create a metafile
                                using (var metafile = new Metafile(screenHdc, rectangle, MetafileFrameUnit.Pixel))
                                {
                                    using (var graphics = Graphics.FromImage(metafile))
                                    {
                                        var hdc = graphics.GetHdc();
                                        //var rect = new Rectangle(0, 0, 320, 50);
                                        OleDraw(webBrowser.ActiveXInstance, DVASPECT_CONTENT, hdc, ref rectangle);
                                        graphics.ReleaseHdc(hdc);
                                    }
                                    // save the metafile as bitmap
                                    metafile.Save(FILE_NAME, ImageFormat.Png);
                                }
                                screenGraphics.ReleaseHdc(screenHdc);
                            }
                        });
    
                        // dispose of webBrowser
                        apartment.Invoke(() => webBrowser.Dispose());
                        webBrowser = null;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
    
            // interop
            const uint DVASPECT_CONTENT = 1;
    
            [DllImport("ole32.dll", PreserveSig = false)]
            static extern void OleDraw(
                [MarshalAs(UnmanagedType.IUnknown)] object pUnk,
                uint dwAspect,
                IntPtr hdcDraw,
                [In] ref System.Drawing.Rectangle lprcBounds);
    
            // WebBrowser Feature Control
            // http://msdn.microsoft.com/en-us/library/ie/ee330733(v=vs.85).aspx
            static void SetFeatureBrowserFeature(string feature, uint value)
            {
                if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                    return;
                var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                Registry.SetValue(@"HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMainFeatureControl" + feature,
                    appName, value, RegistryValueKind.DWord);
            }
    
            public static void GetImage(WebBrowser webBrowser)
            {
                var rectangle = new Rectangle(0, 0, webBrowser.Width, webBrowser.Height);
                //System.Threading.Thread.Sleep(20000);
                // get reference DC
                using (var screenGraphics = webBrowser.CreateGraphics())
                {
                    var screenHdc = screenGraphics.GetHdc();
                    // create a metafile
                    using (var metafile = new Metafile(screenHdc, rectangle, MetafileFrameUnit.Pixel))
                    {
                        using (var graphics = Graphics.FromImage(metafile))
                        {
                            var hdc = graphics.GetHdc();
                            //var rect = new Rectangle(0, 0, 320, 50);
                            OleDraw(webBrowser.ActiveXInstance, DVASPECT_CONTENT, hdc, ref rectangle);
                            graphics.ReleaseHdc(hdc);
                        }
                        // save the metafile as bitmap
                        metafile.Save(FILE_NAME, ImageFormat.Png);
                    }
                    screenGraphics.ReleaseHdc(screenHdc);
                }
            }
        }
    }
  • 相关阅读:
    oracle修改登录认证方式
    oracle设定用户密码使用时间
    oracle口令管理之允许某个用户最多尝试三次登录
    信息系统项目管理师考试大纲(组织整理)
    信息系统项目管理师考试大纲知识点汇总
    项目经理、系统架构师或技术骨干应该具备的知识
    【数据挖掘】数据挖掘工程师是做什么的?
    Java学习
    shuffle的过程分析
    Hadoop 原理总结
  • 原文地址:https://www.cnblogs.com/Googler/p/4091602.html
Copyright © 2020-2023  润新知