• C# 模拟网站登陆并截图


    1、在窗体上加一个按钮,为按钮添加点击事件

            private void button1_Click(object sender, EventArgs e)
            {
                Bitmap m_Bitmap = WebSiteThumbnail.GetWebSiteThumbnail("http://192.168.0.106:1452/", 600, 300, 600, 600);
                //保存图片 JPG、GIF、PNG等均可
                m_Bitmap.Save("F:/Image/" + DateTime.Now.Year.ToString()
                    + DateTime.Now.Month.ToString()
                    + DateTime.Now.Day.ToString()
                     + DateTime.Now.Hour.ToString()
                      + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()
                    + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }

    2、在点击事件中调用了WebSiteThumbnail中的方法,该类的代码如下:

    public class WebSiteThumbnail
        {
            Bitmap m_Bitmap;
            string m_Url;
            int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
           
            public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
            {
                m_Url = Url;
                m_BrowserHeight = BrowserHeight;
                m_BrowserWidth = BrowserWidth;
                m_ThumbnailWidth = ThumbnailWidth;
                m_ThumbnailHeight = ThumbnailHeight;
            }
            public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
            {
                WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
                return thumbnailGenerator.GenerateWebSiteThumbnailImage();
            }
            public Bitmap GenerateWebSiteThumbnailImage()
            {
                Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
                m_thread.SetApartmentState(ApartmentState.STA);
                m_thread.Start();
                m_thread.Join();
                return m_Bitmap;
            }
            private void _GenerateWebSiteThumbnailImage()
            {
                WebBrowser m_WebBrowser = new WebBrowser();
                m_WebBrowser.ScrollBarsEnabled = false;
                m_WebBrowser.Navigate(m_Url);
                m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                    Application.DoEvents();
                m_WebBrowser.Dispose();
            }
            private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                WebBrowser m_WebBrowser = (WebBrowser)sender;
                m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
                m_WebBrowser.ScrollBarsEnabled = false;
                //获取网页文档对象,相当于获取网页的全部源码
                HtmlDocument htmlDoc = m_WebBrowser.Document;
                HtmlElement btn = htmlDoc.GetElementById("Login1_LoginButton");
                if (btn != null)
                {
                    //设置帐号
                    HtmlElement username = htmlDoc.GetElementById("Login1_UserName");
    
                    username.SetAttribute("value", "system");
                    //设置密码
                    HtmlElement pwd = htmlDoc.GetElementById("Login1_Password");
                    pwd.SetAttribute("value", "11111");
    
                    //登录
                    if (btn != null)
                    {
                        btn.InvokeMember("Click");
                        
                    }
                    //进入后的网址
                    m_WebBrowser.Navigate("http://192.168.0.106:1452/Manager.aspx");
                    Thread.Sleep(1000);
                    m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
                    m_WebBrowser.BringToFront();
                    HtmlDocument document = m_WebBrowser.Document;
                    document.Window.ScrollTo(200, 100);
                    m_WebBrowser.DrawToBitmap(m_Bitmap, new Rectangle(new Point(0, 0), new Size(600, 600)));
    
                }
                else
                {
                    m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
                    m_WebBrowser.BringToFront();
                    HtmlDocument document = m_WebBrowser.Document;
                    document.Window.ScrollTo(200, 100);
                    m_WebBrowser.DrawToBitmap(m_Bitmap, new Rectangle(new Point(0, 0), new Size(600, 600)));
    
                }
            }
    
        }
  • 相关阅读:
    练习选择菜单(optionmenu)、上下文菜单(Contextmenu)、弹出菜单(popupmenu)综合小demo
    Androidstudio中listView视图列表控件的使用小练习
    MintUI的MessageBox的用法
    Hbuilder打包app后相机拍摄失效问题的解决
    Vue自带Eslint规范经常报的错误信息
    Object.defineProperty属性实现双向绑定
    移动端开发注意事项
    浏览器兼容问题
    http请求详解
    web页面性能优化及seo
  • 原文地址:https://www.cnblogs.com/zhengwei-cq/p/7435898.html
Copyright © 2020-2023  润新知