if you look back a couple posts you see I got excited about being able to take a snapshot (in bmp form) of a web browser page, well here it is....totally without proper handle closure, exception handling and what-not, here for your entertainment:
mshtml.IHTMLElement iDoc = (mshtml.IHTMLElement)webBrowser1.Document.Body.DomElement;
_IHTMLElementRender iRender = (_IHTMLElementRender)iDoc;
// The 'graphics' object is the dotnet'afied wrapper for the device context with all new, nifty stuff.
// Although this is cool, the functions we'll be using will use the device context directly.
System.Drawing.Graphics gObject = webBrowser1.CreateGraphics();
System.IntPtr dc = gObject.GetHdc(); // get a handle to the underlying device context encapsulated by this object
// take the rendering capability of this object and use it to draw self to the device context (the 'drawing surface').
iRender.DrawToDC( dc);
// The problem is I can't seem to just reach into the data structure of the device
// context and retrieve the bitmap that was drawn there. So, I must get hold of a bitmap
// that is compatible with our device context, add it to a new device context that is compatible
// to the original one, copy the bitmap from the original to the new and, with our handle, reference
// the newly pasted in bitmap (it's actually the same bitmap, just manipulated). We can then
// use this handle to save to disk, etc. I believe that's what's going on, perhaps I'm wrong.
// 1) Take the context of the browser screen and create a new, compatible context for saving our bitmap into.
// CreateCompatibleDC();
System.IntPtr hdc = Native.CreateCompatibleDC(dc);
// 2) Create a bitmap that will work in this context
// CreateCompatibleBitmap():
System.IntPtr hBitmap = Native.CreateCompatibleBitmap(
dc, this.webBrowser1.Width, this.webBrowser1.Height);
// 3) Add the bitmap to the context
// SelectObject();
System.IntPtr hOldObject = Native.SelectObject(
hdc, hBitmap);
// 4) Take the bitmap from the browser and bit-for-bit copy over to the new structure.
// BitBlt();
Native.BitBlt(hdc,
0, 0,
this.webBrowser1.ClientRectangle.Width,
this.webBrowser1.ClientRectangle.Height,
dc,
0, 0,
Native.TernaryRasterOperations.SRCCOPY);
// 5) Take the handle into the new structure we have of our newly created bitmap and do something
// smart with it.
System.Drawing.Bitmap bm = System.Drawing.Image.FromHbitmap(hBitmap);
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
System.String path = saveFileDialog1.FileName;
bm.Save(path);
}