从AutomationElement获取图像类的内容

从AutomationElement获取图像类的内容,automation,ui-automation,automationelement,Automation,Ui Automation,Automationelement,我在包含图像的第三方应用程序中行走控件。automation元素返回类名图像、有关如何将该图像的内容作为位图对象获取的任何想法,甚至字节?此讨论非常有用: 只需使用AutomationElement的BoundingRectangle属性来创建快照。虽然这个问题很老,但当我遇到这个问题时,我想添加一个答案 using System; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Automatio

我在包含图像的第三方应用程序中行走控件。automation元素返回类名图像、有关如何将该图像的内容作为位图对象获取的任何想法,甚至字节?

此讨论非常有用:


只需使用AutomationElement的BoundingRectangle属性来创建快照。

虽然这个问题很老,但当我遇到这个问题时,我想添加一个答案

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;
        }
    }
}
然后,您可以通过将图像称为位图来获取图像

var bitmap = myAutomationElement.ToBitmap();