How do I get a bitmap of the WatiN image element?

前端 未结 5 2112
暗喜
暗喜 2020-12-17 01:27

I have some textfields processed and other elements, but I want to get the bitmap so I can save it somewhere on disk. I need to do it directly from WatiN if this is possible

5条回答
  •  佛祖请我去吃肉
    2020-12-17 02:10

    public Image GetImageFromElement(IHTMLElement Element)
        {
            int width = (int)Element.style.width;
            int height = (int)Element.style.height;
            IHTMLElementRender2 render = (IHTMLElementRender2)Element;
    
            Bitmap screenCapture = new Bitmap(width, height);
            Rectangle drawRectangle = new Rectangle(0, 0, width, height);
            this.DrawToBitmap(screenCapture, drawRectangle);
            Graphics graphics = Graphics.FromImage(screenCapture);
    
            IntPtr graphicshdc = graphics.GetHdc();
            render.DrawToDC(graphicshdc);
            graphics.ReleaseHdc(graphicshdc);
            graphics.Dispose();
    
            return screenCapture as Image;
        }
    

    That's the Method i use for php generated images. It's implimented in my own WebBrowserClass, which extends the webbrowser control.

    (so "this" = WebBrowser)

    But we have to import the IHTMLElementRender2 interface, to use the method.

    [Guid("3050f669-98b5-11cf-bb82-00aa00bdce0b"),
             InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
             ComVisible(true),
             ComImport]
        interface IHTMLElementRender2
        {
            void DrawToDC([In] IntPtr hDC);
            void SetDocumentPrinter([In, MarshalAs(UnmanagedType.BStr)] string bstrPrinterName, [In] IntPtr hDC);
        };
    

    I found this method in web, about 1 year ago, so if you search for it you might find more information.

    Iwan

提交回复
热议问题