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@xxxxxxxxxxxxx> wrote in message
news:1150380786.067068.65110@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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



.



Relevant Pages

  • Re: Urgent: Integrate WebBrowser Control in ASP.NET/VB.NET?
    ... WebRequest/WebResponse to capture the actual HTML of the page (otherwise ... The WebBrowser control is really more for WinForms and displaying HTML and ... Kyril ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Windows Form Dictionary
    ... webBrowser1_Navigating event of the WebBrowser control. ... I mean, i want to capture text, if user double click on it. ... i cant sent it dictionary form. ... Your JavaScript must load a new page, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Urgent: Integrate WebBrowser Control in ASP.NET/VB.NET?
    ... It's in the form of a webservice; but should should be able to adapt it to web page image. ... Scott ... I am going to use it to "capture" an image of a web page. ... > Ok, well, using the WebBrowser control, even in code behind, is going to ...
    (microsoft.public.dotnet.framework.aspnet)
  • How to capture image from WebBrowser control?
    ... Browse to the desired webpage in a WebBrowser control ... I can't manage to capture the displayed image from the WebBrowser ... to save as bitmap. ...
    (microsoft.public.dotnet.framework.drawing)
  • Screen capture of a Form
    ... I would like to take a "screen capture" of just a Form. ... Graphics g2 = Graphics.FromImage; ... IntPtr dc1 = g1.GetHdc; ... // height of destination rectangle ...
    (microsoft.public.dotnet.languages.csharp)