How to capture image from WebBrowser control? - DOTNET

This is a discussion on How to capture image from WebBrowser control? - DOTNET ; Hello all, I need to develop a small application that browses webpages automatically and saves the images found on those pages. Probably you would say: iterate the HtmlElements collection, find the value of the SRC attribute, load each image using ...

+ Reply to Thread
Results 1 to 4 of 4

How to capture image from WebBrowser control?

  1. Default How to capture image from WebBrowser control?

    Hello all,

    I need to develop a small application that browses webpages
    automatically and saves the images found on those pages. Probably you
    would say: iterate the HtmlElements collection, find the value of the
    SRC attribute, load each image using its corresponding URL and then
    save it.

    That doesn't work unfortunately, because the images are generated
    dynamically when requesting the webpage.

    What I did is the following:
    - Browse to the desired webpage in a WebBrowser control
    - Hide all elements except the desired image
    - Resize the WebBrowser to display the full image

    I can't manage to capture the displayed image from the WebBrowser
    control. My WebBrowser control is named wbBrowser

    What I tried is the following:

    Graphics g = wbBrowser.CreateGraphics();
    Bitmap bmp = new Bitmap(width, height);
    g.DrawImage(bmp, 1, 1, width, height);
    bmp.Save("C:\\test_file.jpg",System.Drawing.Imaging.ImageFormat.get_Jpeg());
    g.Dispose();

    It does save the file, but it is completely blank. Its background color
    is black and I see nothing of the 'captured' image in it. I also tried
    to save as bitmap. It saves the file, also completely blank, but with a
    grey background color.

    File size is 9KB using either JPG or BMP format.

    What am I doing wrong here?

    If I'm correct, it should be possible to capture the displayed contents
    of the WebBrowser control as a bitmap and save that to a file.

    Any help would be greatly appreciated.

    Thank you all very much in advance!

    Regards,

    Alex


  2. Default Re: How to capture image from WebBrowser control?

    The code snippet below will capture the rendered HTML page. You can adapt
    it to your needs:

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

    private void axWebBrowser1_DocumentComplete(object sender,
    AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
    {
    AxSHDocVw.AxWebBrowser wb = (AxSHDocVw.AxWebBrowser)sender;
    IHTMLDocument3 doc = (IHTMLDocument3)wb.Document;
    if ( doc != null )
    {
    IHTMLElement3 el = (IHTMLElement3)doc.documentElement;

    if ( el != null )
    {
    IHTMLElementRender Render = (IHTMLElementRender)el;
    Graphics g = Graphics.FromImage(bm); // a bitmap object that was
    previously created to the desired width, height and bit depth.
    IntPtr hdc = g.GetHdc();
    Render.DrawToDC(hdc);
    g.ReleaseHdc(hdc);
    g.Dispose();
    Marshal.ReleaseComObject(Render);
    bm.Save(@"test.bmp",ImageFormat.Bmp);
    }
    }
    }

    "StartPixel" <alex@waveglobal.nl> wrote in message
    news:1150380786.067068.65110@r2g2000cwb.googlegroups.com...
    > Hello all,
    >
    > I need to develop a small application that browses webpages
    > automatically and saves the images found on those pages. Probably you
    > would say: iterate the HtmlElements collection, find the value of the
    > SRC attribute, load each image using its corresponding URL and then
    > save it.
    >
    > That doesn't work unfortunately, because the images are generated
    > dynamically when requesting the webpage.
    >
    > What I did is the following:
    > - Browse to the desired webpage in a WebBrowser control
    > - Hide all elements except the desired image
    > - Resize the WebBrowser to display the full image
    >
    > I can't manage to capture the displayed image from the WebBrowser
    > control. My WebBrowser control is named wbBrowser
    >
    > What I tried is the following:
    >
    > Graphics g = wbBrowser.CreateGraphics();
    > Bitmap bmp = new Bitmap(width, height);
    > g.DrawImage(bmp, 1, 1, width, height);
    > bmp.Save("C:\\test_file.jpg",System.Drawing.Imaging.ImageFormat.get_Jpeg());
    > g.Dispose();
    >
    > It does save the file, but it is completely blank. Its background color
    > is black and I see nothing of the 'captured' image in it. I also tried
    > to save as bitmap. It saves the file, also completely blank, but with a
    > grey background color.
    >
    > File size is 9KB using either JPG or BMP format.
    >
    > What am I doing wrong here?
    >
    > If I'm correct, it should be possible to capture the displayed contents
    > of the WebBrowser control as a bitmap and save that to a file.
    >
    > Any help would be greatly appreciated.
    >
    > Thank you all very much in advance!
    >
    > Regards,
    >
    > Alex
    >




  3. Default Re: How to capture image from WebBrowser control?

    hai , i have done it in visual basic.


    you can mail me at somala_pratap@yahoo.co.in

    thnaks
    pratap
    ---
    Posted via www.DotNetSlackers.com

  4. Default Re: How to capture image from WebBrowser control?

    hai

    i have done it in visual basic.

    you can mail me at somala_pratap@yahoo.co.in

    thanks

    ---
    Posted via www.DotNetSlackers.com

+ Reply to Thread

Similar Threads

  1. Local Image on a WebBrowser control
    By Application Development in forum DOTNET
    Replies: 5
    Last Post: 12-05-2007, 02:54 AM
  2. Image Capture Control for Windows Mobile using DirectShow
    By Application Development in forum CSharp
    Replies: 4
    Last Post: 11-14-2007, 04:27 PM
  3. CSS background image on webbrowser control
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 09-11-2007, 09:39 PM
  4. Capture Image of Control to Clipboard
    By Application Development in forum CSharp
    Replies: 1
    Last Post: 08-24-2007, 05:30 AM
  5. Using ExecWB to control font size in a WebBrowser control
    By Application Development in forum basic.visual
    Replies: 2
    Last Post: 07-01-2007, 03:44 PM