Re: How to capture image from WebBrowser control?
- From: "Michael Phillips, Jr." <mphillips53@xxxxxxxxxxxxxxx>
- Date: Thu, 15 Jun 2006 11:13:15 -0400
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
.
- References:
- How to capture image from WebBrowser control?
- From: StartPixel
- How to capture image from WebBrowser control?
- Prev by Date: Re: Performance & new Pen() / new Brush()
- Next by Date: DataGridView, How to display grid lines in all "rows"?
- Previous by thread: How to capture image from WebBrowser control?
- Next by thread: DataGridView, How to display grid lines in all "rows"?
- Index(es):
Relevant Pages
|