Wednesday 9 October 2013

Save Image from Web Browser Control C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
class HTMLImageCap
{
    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }
    //use win32 api
    private class Gdi32
    {
        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
    }

    private static class User32
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        [DllImport("user32.dll")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
    }

    //define constants
    private const int SRCCOPY = 13369376;
    private const int LOOPTIMES = 40;
    private const int SLEEPTIME = 100;

    private static int getXoffset(HtmlElement he)
    {
        //get element pos
        int xPos = he.OffsetRectangle.Left;

        //get the parents pos
        HtmlElement tempEl = he.OffsetParent;
        while (tempEl != null)
        {
            xPos += tempEl.OffsetRectangle.Left;
            tempEl = tempEl.OffsetParent;
        }

        return xPos;
    }

    private static int getYoffset(HtmlElement he)
    {
        //get element pos
        int yPos = he.OffsetRectangle.Top;

        //get the parents pos
        HtmlElement tempEl = he.OffsetParent;
        while (tempEl != null)
        {
            yPos += tempEl.OffsetRectangle.Top;
            tempEl = tempEl.OffsetParent;
        }

        return yPos;
    }

    private static Image captureWindow(IntPtr handle, int x, int y, int width, int height)
    {

        IntPtr hdcSrc = User32.GetWindowDC(handle);

        RECT windowRect = new RECT();
        User32.GetWindowRect(handle, ref windowRect);
        IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
        IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

        IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
        Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, SRCCOPY);
        Gdi32.SelectObject(hdcDest, hOld);
        Gdi32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);
        Image img = Image.FromHbitmap(hBitmap);
        Gdi32.DeleteObject(hBitmap);
        return img;
    }
    public static Image getImage(WebBrowser wb, HtmlElement he)
    {
        //get original scroll position
        int docY = wb.Document.GetElementsByTagName("HTML")[0].ScrollTop;
        int docX = wb.Document.GetElementsByTagName("HTML")[0].ScrollLeft;

        //scroll to view element
        wb.Document.Window.ScrollTo(getXoffset(he), getYoffset(he));
        int heY = getYoffset(he) - wb.Document.GetElementsByTagName("HTML")[0].ScrollTop;
        int heX = getXoffset(he) - wb.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
        int heWidth = he.OffsetRectangle.Width;
        int heHeight = he.OffsetRectangle.Height;

        Image img = captureWindow(wb.Handle, heX, heY, heWidth, heHeight);

        //scroll back tooriginal position
        wb.Document.Window.ScrollTo(docX, docY);
        return img;

    }
}
Put this class in your project and use like this
 string Url = "https://accounts.google.com/SignUp";
webBrowser1.Navigate(Url);
            //Wait for few secs
HtmlElement sliderWrap = webBrowser1.Document.GetElementById("recaptcha_image");
Image i = HTMLImageCap.getImage(webBrowser1, sliderWrap);
i.Save(Application.StartupPath + "\\text.jpg");

2 comments:

  1. Added the ability to add to the element offset of frames

    ReplyDelete
  2. Thanks!!!!! It works.

    Funfou certinho mano!

    ReplyDelete