Joga
Joga

Reputation: 245

UI Automation : Save Automation Element as image?

I have an use case as follows,

    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"));

The above code fetches me the imageData from my UI. Is there a way for me to save the imageData (of "Image" type) as a file. I tried using the screen capture method, but since I have my app with scroll bar enabled, taking a screen shot doesnt work properly.

Is there a way to save image data to file from automation element of an image? TIA.

Upvotes: 1

Views: 1243

Answers (1)

daidmelad
daidmelad

Reputation: 19

If you will use TestStack.White there is a Image class that you can use to get image bytes, the property VisibleImage return Bitmap object and then you can use ImageConverter that will convert bitmap into bytes array and then using simple File.IO and method WriteAllBytes you can save the image into file.

       var visibleImage = ChangeIcon.VisibleImage;

        ImageConverter imgConverter = new ImageConverter();
        var bytes =  (byte[])imgConverter.ConvertTo(visibleImage, typeof(byte[]));            

        File.WriteAllBytes($"icon_{new Random().Next()}.bmp", bytes);            

(the ChangeIcon property type is Image)

Upvotes: 1

Related Questions