Custom C# code - take desktop screenshot - throws "Handle is invalid" exception
We are using Pega robotics (RPA) for automating web application process as part of our our Process automation. We are using Pega robotics version 19.1.89.
While using a customized C# code to take screenshots, we get the following error at some places.
Error message while trying to capture screenshot The handle is invalid--FullException::System.ComponentModel.Win32Exception (0x80004005): The handle is invalid at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation) at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize) at Dynamic.Script_8D5DAC3DB8A72BF.Script.GrabScreenShotPNG(String outputImagePath, Int32 width, Int32 height, Int32 xCoordinate, Int32 yCoordinate)--Inner Exception::
This is the logic that we use to take the screenshot
We are using Pega robotics (RPA) for automating web application process as part of our our Process automation. We are using Pega robotics version 19.1.89.
While using a customized C# code to take screenshots, we get the following error at some places.
Error message while trying to capture screenshot The handle is invalid--FullException::System.ComponentModel.Win32Exception (0x80004005): The handle is invalid at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation) at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize) at Dynamic.Script_8D5DAC3DB8A72BF.Script.GrabScreenShotPNG(String outputImagePath, Int32 width, Int32 height, Int32 xCoordinate, Int32 yCoordinate)--Inner Exception::
This is the logic that we use to take the screenshot
using( Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)) { using( Graphics graphics = Graphics.FromImage(bitmap)) { graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); } bitmap.Save(outputImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); }