Posted By: Void | Sep 13th, 2004 @ 2:37 PM
page 1 of 1
Comments: 13 | Views: 27096
Void
Void
A state of nonexistence
I'm working on a quick C# project where I use a web browser control to render some HTML on a form.  I need to create an image of the control that I can save to the disk as a bitmap.  To make this clear..  Say I used the browser control to bring up Google..  I want to save an image (BMP, GIF, JPEG preferred) of the complete page with all it's text and graphics combined.  I could even get by with just saving the part that's in view on the control. The key idea here is that I want to render some HTML then save it as a bitmap.
 
How do I get a bitmap from the web browser control?  I can get a graphics object, but I don't know how to get a bitmap from that either.  Any help?
 
Void
qwert231
qwert231
M Kenyon
Here's what I would do...
Browse to the screen you're looking at.
Press Print Screen
Open a new image file
Click 'Edit' and 'Paste'
I'm pretty sure that the MSHTML library exposes some COM classes/interfaces that you can use to load a page and render it directly to a bitmap at any size you desire. Not sure, since I haven't done it, but if you haven't checked, dig around.
Void wrote:
I am rendering some dynamic HTML to a bitmap once per hour.  I must do this programaticaly on a server.

Looks like this will help:
http://www.codeproject.com/internet/htmlimagecapture.asp
was anyone able to get this working in C#? I amlooking for something similar to the one in codeproject but without having to display the browser control to the user
After some digging, I found out how to do it: http://www.developerfusion.co.uk/show/4712/

Im also trying to do this. Please send me a working sample as Im just new to C#

Matt

stoic4321
stoic4321
At first, I thought it was a bug.
I found this nifty explanation here:
http://xidey.wordpress.com/2007/12/12/the-missing-drawtobitmap-function-in-the-net-webbrowser-class/

"
 got this function (WebBrowser.DrawToBitmap) to work.

We have to follow these points to get it working
1. The webbrowser control should not be dragged on form, Rather it should be instantiate in code.
2. The function DrawToBitmap should only be called in DocumentCompleted eventhandler.

Here is sample code

public partial class Form1 : Form
{
  WebBrowser wb = new WebBrowser();

  public Form1()
  {
    InitializeComponent();

    wb.Height = 100;
    wb.Width = 100;
    wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
  }

  void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  {
    Bitmap bmp = new Bitmap(100, 100);
    wb.DrawToBitmap(bmp, new Rectangle(wb.Location.X, wb.Location.Y, wb.Width, wb.Height));

    this.pictureBox1.BackgroundImage = bmp;
  }

}
"

Hope that helps.
-SB =]

Can Anybody help me out in setting webbrowser control project on localhost(Virtual directory ) . i am successful in using webbrowser control on web application but it runs on file system or web application with some specific port defined. how can i use by configuring it on virtual directory without any port .
Thankx in Advance

Ion Todirel
Ion Todirel
ban...kai
there's always a port on which the web server listens for the incoming connections, the default used is 80, and you don't need to specify it in that case, if you would be more specific...