I am taking screenshot during UI Test execution by Azure Agent.
For some reason, this line never completes, there is no error, no exception it just waits on it indefinitely:
g.CopyFromScreen(Point.Empty, Point.Empty, new Size(recorderParams.SourceWidth, recorderParams.SourceHeight), CopyPixelOperation.SourceCopy);
This code is run in a separate thread:
captureFrameThread = new Thread(TakeScreenshot)
{
IsBackground = false
};
captureFrameThread.Start();
and the full methods looks like this:
public byte[] TakeScreenshot()
{
byte[] buffer = new byte[recorderParams.SourceWidth * recorderParams.SourceHeight * 4];
HooksSetup.AppendToFile("Taken screenshot 1");
using (var bmp = new Bitmap(recorderParams.SourceWidth, recorderParams.SourceHeight))
{
using (var g = Graphics.FromImage(bmp))
{
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
// it gets stuck on a call below
g.CopyFromScreen(Point.Empty, Point.Empty, new Size(recorderParams.SourceWidth, recorderParams.SourceHeight), CopyPixelOperation.SourceCopy);
g.Flush();
var bits = bmp.LockBits(new Rectangle(0, 0, recorderParams.SourceWidth, recorderParams.SourceHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);
bmp.UnlockBits(bits);
}
}
return buffer;
}
On my local machine this code always works flawlessly.
Why is it so? What is my alternative?