views:

45

answers:

1

I'm using a WebBrowser control and want to copy a particular image on the web page to the clipboard. I am aware that I can use the WebBrowser.Document.ExecCommand method to copy the currently selected region of the page but cannot work out how to direct the selection to cover a particular HtmlElement or region of the page.

Any help is much appreciated!

A: 

I didn't write this but found it a while ago and thought it would help you out.

http://www.codeproject.com/Messages/3206780/Re-Image-in-WebBrowser.aspx

IHTMLDocument2 doc = (IHTMLDocument2) webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange) ((HTMLBody) doc.body).createControlRange();

foreach (IHTMLImgElement img in doc.images)
{
  imgRange.add((IHTMLControlElement) img);

  imgRange.execCommand("Copy", false, null);

  using (Bitmap bmp = (Bitmap) Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
  {
    bmp.Save(@"C:\"+img.nameProp);
  }
}
Justin
Thanks! Just for future reference for anyone else using this though - you'll need to add reference to Microsoft.mshtml to your project and add "using mshtml" to your class file.
JoeR
@JoeR, thanks for the info, this is pretty old and I haven't used the code in a while but I recalled having it around after reading your question.
Justin