Screen capture control using WebBrowser control and GDI+ in a web app

Discussion in 'ASP .Net' started by JP2006, Jan 7, 2007.

  1. JP2006

    JP2006 Guest

    I'm trying to write a control that will take a screen capture of a
    particular website when a user submits a form in a web application; one
    of the form fields is for a URL - the control needs to get an image of
    that web site so that it can be displayed later as a thumbnail image.

    I have code for taking a normal screen capture using GDI+ which works
    fine. What I am now trying to do is to modify it so the screen capture
    is of a remote website rather than of my PC. To attempt to do this I am
    using the .NET WebBrowser control to load the website URL into.

    The main bit of code is:


    // The method that is called from the ASPX page when the form is
    submitted
    public static void newxcap2()
    {
    WebBrowser m_browser = new WebBrowser();
    m_browser.Visible = false;
    m_browser.DocumentCompleted += new
    WebBrowserDocumentCompletedEventHandler(GetScreenCapture);
    m_browser.Navigate(m_browser.Url = new
    Uri("http://www.bbc.co.uk
    }

    public static void GetScreenCapture(object sender,
    WebBrowserDocumentCompletedEventArgs e)
    {
    Graphics _g = ((WebBrowser)sender).CreateGraphics();
    System.IntPtr desktopDC = GetDC(System.IntPtr.Zero);
    Image bm = new Bitmap(300, 300, _g);
    Graphics g = Graphics.FromImage(bm);
    System.IntPtr bmDC = g.GetHdc();
    BitBlt(bmDC, 0, 0, bm.Width,bm.Height, desktopDC, 0, 0,
    0x00CC0020 /*SRCCOPY*/);
    ReleaseDC(System.IntPtr.Zero, desktopDC);
    g.ReleaseHdc(bmDC);
    g.Dispose();
    bm.Save(@"C:\Documents and
    Settings\JP\Desktop\xCapImage.bmp");
    }

    Having tinkered around with it quite a bit the most I can make it do is
    to generate a blank image, other attempts have resulted in various
    errors.

    Am I on the right track in general here with the use of the WebBrowser
    control (to in effect load a webpage behind the scenes) or am I
    approaching this in the wrong way?
    Thanks

    James
     
    JP2006, Jan 7, 2007
    #1
    1. Advertising

  2. Uhm I'm surprised you're even getting that far. I suspect the Web Browser
    requires a hosting container to work with a window. I don't think it will
    render unless there's a Windows event loop to refresh the display area...
    You can check and see if that's the issue by creating a form and then from
    within ASP.NET fire that form with a full Application.Run event loop. That
    *might* work better, but even then this might not work. Remember ASP.NET
    runs as a service in a system context not on the desktop.

    ASP.NET is not really a good way to do this.Aside from the security issues,
    this seems like a task for a WinForm application...

    +++ Rick ---

    --
    Rick Strahl
    West Wind Technologies
    www.west-wind.com/weblog



    "JP2006" <> wrote in message
    news:...
    > I'm trying to write a control that will take a screen capture of a
    > particular website when a user submits a form in a web application; one
    > of the form fields is for a URL - the control needs to get an image of
    > that web site so that it can be displayed later as a thumbnail image.
    >
    > I have code for taking a normal screen capture using GDI+ which works
    > fine. What I am now trying to do is to modify it so the screen capture
    > is of a remote website rather than of my PC. To attempt to do this I am
    > using the .NET WebBrowser control to load the website URL into.
    >
    > The main bit of code is:
    >
    >
    > // The method that is called from the ASPX page when the form is
    > submitted
    > public static void newxcap2()
    > {
    > WebBrowser m_browser = new WebBrowser();
    > m_browser.Visible = false;
    > m_browser.DocumentCompleted += new
    > WebBrowserDocumentCompletedEventHandler(GetScreenCapture);
    > m_browser.Navigate(m_browser.Url = new
    > Uri("http://www.bbc.co.uk
    > }
    >
    > public static void GetScreenCapture(object sender,
    > WebBrowserDocumentCompletedEventArgs e)
    > {
    > Graphics _g = ((WebBrowser)sender).CreateGraphics();
    > System.IntPtr desktopDC = GetDC(System.IntPtr.Zero);
    > Image bm = new Bitmap(300, 300, _g);
    > Graphics g = Graphics.FromImage(bm);
    > System.IntPtr bmDC = g.GetHdc();
    > BitBlt(bmDC, 0, 0, bm.Width,bm.Height, desktopDC, 0, 0,
    > 0x00CC0020 /*SRCCOPY*/);
    > ReleaseDC(System.IntPtr.Zero, desktopDC);
    > g.ReleaseHdc(bmDC);
    > g.Dispose();
    > bm.Save(@"C:\Documents and
    > Settings\JP\Desktop\xCapImage.bmp");
    > }
    >
    > Having tinkered around with it quite a bit the most I can make it do is
    > to generate a blank image, other attempts have resulted in various
    > errors.
    >
    > Am I on the right track in general here with the use of the WebBrowser
    > control (to in effect load a webpage behind the scenes) or am I
    > approaching this in the wrong way?
    > Thanks
    >
    > James
    >
     
    Rick Strahl [MVP], Jan 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    7
    Views:
    9,126
  2. Robin van de Water [synthesiSFactory]

    GDI+ is not properly initialized (internal GDI+ error).

    Robin van de Water [synthesiSFactory], Jan 10, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    1,350
    Robin van de Water [synthesiSFactory]
    Jan 10, 2007
  3. hemant
    Replies:
    1
    Views:
    751
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jan 26, 2007
  4. Replies:
    2
    Views:
    2,424
    Jim Langston
    Jul 7, 2006
  5. Replies:
    5
    Views:
    930
Loading...

Share This Page