C# UI自动化:是否将自动化元素另存为图像?
我有一个用例,如下所示C# UI自动化:是否将自动化元素另存为图像?,c#,wpf,ui-automation,coded-ui-tests,C#,Wpf,Ui Automation,Coded Ui Tests,我有一个用例,如下所示 Process proc = Process.Start("myproc.exe"); AutomationElement automationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new System.Windows.Automation.PropertyCondition(AutomationElement.Proces
Process proc = Process.Start("myproc.exe");
AutomationElement automationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new System.Windows.Automation.PropertyCondition(AutomationElement.ProcessIdProperty,
proc.Id));
var imageAutomationElmt = automationElement .FindFirst
(TreeScope.Descendants, new System.Windows.Automation.PropertyCondition
(AutomationElement.AutomationIdProperty, "imageData"));
上面的代码从UI中获取imageData。是否有办法将imageData(图像类型)保存为文件。我尝试使用屏幕捕获方法,但由于我的应用程序启用了滚动条,因此拍摄屏幕快照无法正常工作
有没有办法将图像数据从图像的自动化元素保存到文件中?
TIA.如果要使用TestStack.White,可以使用Image类获取图像字节、属性VisibleImage return Bitmap对象,然后可以使用ImageConverter将位图转换为字节数组,然后使用simple File.IO和方法WriteAllBytes将图像保存到文件中
var visibleImage = ChangeIcon.VisibleImage;
ImageConverter imgConverter = new ImageConverter();
var bytes = (byte[])imgConverter.ConvertTo(visibleImage, typeof(byte[]));
File.WriteAllBytes($"icon_{new Random().Next()}.bmp", bytes);
(ChangeIcon属性类型为Image)将整个控件呈现为位图非常容易,但我不确定您是否可以将AutomationElement强制转换为WPF控件。像snoop这样的项目能够呈现正在运行的应用程序的接口,可能值得通过它的代码(它的开源)来了解它们是如何做到的。一旦您将对象作为控件,其余的将很容易。您已经在此处提出了相同的问题: