Search code examples
tfsscreenshot

Using c# code how can we Capture screenshot of a VSTS site


We have Visual Studio Team Services (VSTS) site. We need to take the screenshots of the specific regions on the page using c# code. Tried out to fetch the html content of the page. However unable to do so even though we are giving proper credentials.

Require help to take screenshot of the VSTS site using c# code (of specific region)

I used below code to try to fetch the html content of the page. (So that once I get the html content I can convert it to image).

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

If I give the URL as Google or any other URL then it working fine however if I give the VSTS site URL even with credentials it is not working


Solution

  • Not sure why you need to capture the screenshot for VSTS from C# code. But the issue you meet could be caused by authentication in your code. When you authenticate to VSTS from 3rd party tools or code, you need to enable alternate credential from "VSTS Web Portal/Security/Alternate authentication credentials" and use the alternate credential for the authentication. So update your code to below and then try again:

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

    Another thing is that you may still meet some other issue even you can authenticate to VSTS successfully since the response from VSTS is more complex than Google. I would recommend you to use some web automation testing tools like Selenium to achieve the feature you want.

    Following code also works at my side:

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