【问题标题】:C# .net screencapture from hidden site. [duplicate]C# .net 来自隐藏站点的屏幕截图。 [复制]
【发布时间】:2015-11-19 21:35:10
【问题描述】:

正在网页中生成图表。我正在尝试使用 C# 代码进行屏幕截图。我尝试了下面的代码。它工作了几次,现在我得到“索引超出范围。必须是非负数并且小于集合的大小”错误。

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

    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;
    }

【问题讨论】:

  • 您介意指出引发该错误的行吗?
  • 你在哪一行得到这个错误
  • 您了解“范围索引”错误的含义吗?您有兴趣找出答案,还是只想让您复制+粘贴的代码再次工作?
  • @Blorgbeard,是的,我知道错误是什么。正如我们所见,我的代码没有列表或数组。
  • 罗恩和@mohit。那个问题是它没有抛出异常或在任何地方中断。如果我可以做一些调试。

标签: c# .net multithreading thread-sleep screen-capture


【解决方案1】:

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

        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);
       }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多