来自隐藏站点的c# .net屏幕截图

本文关键字:net 屏幕截图 站点 隐藏 | 更新日期: 2023-09-27 18:10:01

网页中正在生成图形。我试图做一个屏幕截图与c#代码。我尝试了下面的代码。它工作了几次,现在我得到"index out of range"。必须非负且小于集合的大小"错误。

感谢任何帮助或指导。提前感谢。

    System.Drawing.Bitmap bitMap = null;
    static AutoResetEvent autoEvent;
    public void DownloadAsImage(string title, string location) 
    {
        autoEvent = new AutoResetEvent(false);
        Uri url = HttpContext.Current.Request.Url;
        string RootUrl = url.AbsoluteUri.Replace(url.PathAndQuery, string.Empty);
        //ApartmentState:specifies the state of a system.threading.thread
        //STA:Thread will create and enter a single-threaded apartment
        Thread t = new Thread(CaptureWebPageToDisplay);
        t.SetApartmentState(ApartmentState.STA);
        if (title == "PieGraph" && location == "0")
        {
            t.Start(RootUrl + "/_Layouts/AppPage/Reports/TotalGraphPage.aspx?isdlg=1");
            Thread.Sleep(30000);
        }
        else
        {
            t.Start(RootUrl + "/_Layouts/AppPage/Reports/GraphPage.aspx?isdlg=1&Title=" + title + "&Location=" + location);
            Thread.Sleep(10000);
        }
        autoEvent.Set();
        HttpContext.Current.Response.ContentType = "image/jpeg";
        string targetFolder = Server.MapPath(@".'GraphImages'") + title + ".Jpeg";
        try
        {
            bitMap.Save(targetFolder);
            bitMap.Dispose();
        }
        catch(Exception ex){
            DBLogger.ExpandException(ex);
            if (bitMap != null)
            {
                bitMap.Dispose();
            }
        }
    }
    public void CaptureWebPageToDisplay(object URL)
    {
        // create a hidden web browser, which will navigate to the page 
        System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
        // Full web browser
        web.Dock = System.Windows.Forms.DockStyle.Fill;
        web.Size = new System.Drawing.Size(1000, 800);
        // we don't want scrollbars on our image 
        web.ScrollBarsEnabled = false;
        // don't let any errors shine through 
        web.ScriptErrorsSuppressed = true;
        // let's load up that page! 
        web.Navigate((string)URL);
        // wait until the page is fully loaded 
        while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) 
            System.Windows.Forms.Application.DoEvents();
        System.Threading.Thread.Sleep(5000); // allow time for page scripts to update 

        // the appearance of the page 
        // set the size of our web browser to be the same size as the page 
        int width = web.Document.Body.ScrollRectangle.Width;
        int height = web.Document.Body.ScrollRectangle.Height;
        web.Width = width;
        web.Height = height;
        // a bitmap that we will draw to 
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
        // change background color to white, just in case
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
        g.Clear(System.Drawing.Color.White);
        // create rectangle.
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(20, 0, width, height);
        // draw the web browser to the bitmap 
        web.DrawToBitmap(bmp, rect);
        bitMap = bmp;
    }

来自隐藏站点的c# .net屏幕截图

我认为下面的代码捕获屏幕可能会有帮助:

        public static void CaptureScreen(double x, double y, double width, double height)
        {
            int ix, iy, iw, ih;
            ix = Convert.ToInt32(x);
            iy = Convert.ToInt32(y);
            iw = Convert.ToInt32(width);
            ih = Convert.ToInt32(height);
            Bitmap image = new Bitmap(iw, ih,
                   System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(image);
            g.CopyFromScreen(ix, iy, ix, iy,
                     new System.Drawing.Size(iw, ih),
                     CopyPixelOperation.SourceCopy);
            // Download Image
            image.Save(FileName, ImageFormat.Png);
       }