I am walking controls in a third party app that contains images. The automation element returns the class name Image, Any ideas on how I can get the contents of that Image as a bitmap object, or even bytes?
Asked
Active
Viewed 731 times
2 Answers
1
Although this question is old, I wanted to add an answer as I came across this problem as well.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Automation;
namespace AutomationElementExtension
{
public static class AutomationElementExtension
{
public static Bitmap ToBitmap(this AutomationElement automationElement)
{
var boundingRectangle = automationElement.Current.BoundingRectangle;
var bitmap = new Bitmap(Convert.ToInt32(boundingRectangle.Width), Convert.ToInt32(boundingRectangle.Height), PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(Convert.ToInt32(boundingRectangle.X), Convert.ToInt32(boundingRectangle.Y), Point.Empty.X, Point.Empty.Y, bitmap.Size);
}
return bitmap;
}
}
}
You can then get the image as a bitmap by calling it as
var bitmap = myAutomationElement.ToBitmap();

bergmeister
- 949
- 2
- 10
- 16
0
This discussion can be useful: Capturing an image behind a rectangle .
Just use BoundingRectangle property of the AutomationElement to make a snapshot.

Community
- 1
- 1

Vasily Ryabov
- 9,386
- 6
- 25
- 78