WatiN NativeElement.GetElementBounds() - What is the unit of measurement?
        Posted  
        
            by Brian Schroer
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Brian Schroer
        
        
        
        Published on 2010-05-17T16:12:25Z
        Indexed on 
            2010/05/31
            16:32 UTC
        
        
        Read the original article
        Hit count: 502
        
When I'm testing with WatiN, I like to save screenshots. Sometimes I don't really need a picture of the whole browser window though - I just want a picture of the element that I'm testing.
My attempt to save a picture of an element with the code below resulted in a picture of a block box, because elementBounds.Top points to a pixel position way past the bottom of the screen. The elementBounds.Width and .Height values also appear to be about half what they should be.
Is this a WatiN bug, or are these properties in a different unit of measure that I have to convert to pixels somehow?
public static void SaveElementScreenshot
    (WatiN.Core.IE ie, WatiN.Core.Element element, string screenshotPath)
{
    ScrollIntoView(ie, element);
    ie.BringToFront();
    var ieClass = (InternetExplorerClass) ie.InternetExplorer;
    Rectangle elementBounds = element.NativeElement.GetElementBounds();
    int left = ieClass.Left + elementBounds.Left;
    int top = ieClass.Top + elementBounds.Top;
    int width = elementBounds.Width;
    int height = elementBounds.Height;
    using (var bitmap = new Bitmap(width, height))
    {
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            graphics.CopyFromScreen
                (new Point(left, top), Point.Empty, new Size(width, height));
        }
        bitmap.Save(screenshotPath, ImageFormat.Jpeg);
    }
}
        © Stack Overflow or respective owner