【问题标题】:Using c# code how can we Capture screenshot of a VSTS site使用 c# 代码我们如何捕获 VSTS 站点的屏幕截图
【发布时间】:2016-12-03 13:29:43
【问题描述】:

我们有 Visual Studio Team Services (VSTS) 网站。我们需要使用 c# 代码对页面上的特定区域进行截图。尝试获取页面的 html 内容。但是,即使我们提供了正确的凭据,也无法这样做。

需要帮助使用 c# 代码(特定区域)截取 VSTS 站点的屏幕截图

我使用下面的代码来尝试获取页面的 html 内容。 (这样一旦我获得了 html 内容,我就可以将其转换为图像)。

WebRequest request = WebRequest.Create("google.com"); WebResponse response = request.GetResponse(); 
Stream data = response.GetResponseStream();
string html = String.Empty; 

using (StreamReader sr = new StreamReader(data)) 
{ html = sr.ReadToEnd(); }

如果我将 URL 作为 Google 或任何其他 URL 提供,那么它可以正常工作,但是如果我提供 VSTS 站点 URL,即使使用凭据它也无法正常工作

【问题讨论】:

  • 您的问题不清楚。请改写...
  • 截图不是你的意思。打开截图工具并创建所需区域的图像即可轻松实现屏幕截图。
  • 即使有多个图表,您是否只想在网络上导入一个图表。
  • 更新了描述和标题

标签: tfs screenshot


【解决方案1】:

不确定为什么需要从 C# 代码捕获 VSTS 的屏幕截图。但是您遇到的问题可能是由代码中的身份验证引起的。当您从 3rd 方工具或代码向 VSTS 进行身份验证时,您需要从“VSTS Web Portal/Security/Alternate authentication credentials”启用备用凭据,并使用备用凭据进行身份验证。所以更新你的代码到下面然后再试一次:

            string altusername = "xxx";
            string altpassword = "xxx";
            WebRequest request = WebRequest.Create("https://xxx.visualstudio.com/xxx/xxx");
            string auth = altusername + ":" + altpassword;
            auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
            request.Headers["Authorization"] = "Basic" + auth;

            WebResponse response = request.GetResponse();
            Stream data = response.GetResponseStream();
            string html = String.Empty;

            using (StreamReader sr = new StreamReader(data))
            { html = sr.ReadToEnd(); }

另一件事是,即使您可以成功通过 VSTS 身份验证,您仍可能遇到其他问题,因为 VSTS 的响应比 Google 更复杂。我建议你使用一些网络自动化测试工具,如Selenium 来实现你想要的功能。

以下代码也适用于我:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
    {
        string altusername = "xxx";
        string altpassword = "xxx";
        string url = "https://xxx.visualstudio.com/_git/Python";
        string auth = altusername + ":" + altpassword;
        auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
        string header = "Authorization: Basic " + auth;
        webBrowser1.Navigate(url, null,null,header);
        Timer tim = new Timer();
        tim.Tick += new EventHandler(timer_Tick); 
        tim.Interval = (1000) * (30);             // Adjust the time base on the time you need to load the webpage
        tim.Enabled = true;                       
        tim.Start();                              
    }
    void timer_Tick(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(webBrowser1.Width, webBrowser1.Height);
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        webBrowser1.DrawToBitmap(bmp, rect);
        bmp.Save("D:\\Code\\0a.bmp");
    }
}

【讨论】:

  • 感谢您的帮助。尝试了上述代码,但出现以下错误:“Microsoft Internet Explorer 的增强安全配置当前已在您的环境中启用。这种增强的安全级别会阻止我们的 Web 集成体验正确显示或执行。请继续您的操作禁用此配置或联系您的管理员。”。尝试按照“4sysops.com/archives/…”上的步骤进行操作。但是无法实现它
  • @nina 无法打开您提到的链接。您可以通过以下链接尝试禁用 IE 增强安全配置吗:limestonenetworks.com/support/knowledge-center/17/70/…
  • 使用上述 url 禁用增强的安全配置。但是仍然得到同样的错误。我复制了从代码中获取的 html 内容并将其粘贴到 html 文件中。然后打开html文件。因此出现错误“Internet Explorer 限制此网页运行脚本或 ActiveX 控件”。跟随网址“stackoverflow.com/questions/7038724/…”。但是仍然存在同样的问题
  • @nina 你能直接从浏览器打开VSTS网站吗?您可以从其他浏览器(例如 Chrome)打开它吗?
  • 可以从浏览器打开
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 2015-04-06
  • 1970-01-01
  • 2013-10-08
相关资源
最近更新 更多