0

Hi i have a c# IE automation script and i am trying to retrieve the image from a HTMLImgClass, i cant get the image from cache because its not saved and i cant just resend a request to the src because a new image returns, so i need a way to access the Image in the browsers memory.

captcha_image = (HTMLImgClass)GetElementByPosition("img", 0, ie1);

The object is retreived with the above assignement, which works fine, but i have no idea which of the methods available i can use to get the image.

Thanx for your time

SOLVED For others interested, i solved it this way, i decided to copy the image to clipboard and then save it as a bmp

captcha_image = (HTMLImgClass)GetElementByPosition("img", 0, ie1);
                                    IHTMLImgElement captcha_image1 = (IHTMLImgElement)captcha_image;
                                    IHTMLDocument2 doc = (IHTMLDocument2)wb1.Document;
                                    IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
                                    imgRange.add((IHTMLControlElement)captcha_image1);

                                    imgRange.execCommand("Copy", false, null);
                                    using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                                    {
                                        bmp.Save(@"C:\skt.bmp");
                                    }
Evan
  • 1,683
  • 7
  • 35
  • 65

1 Answers1

0

I'm assuming that this is related to your other question. As suggested in my answer there, if the image hasn't been saved to disk cache, you won't be able to access it there.

A solution is to force your browser to go through a proxy for all requests, and therefore to store the captcha images as they are being requested in the first place. I've used Fiddler2 for this in the past.

In Fiddler2, you can change the OnBeforeResponse hook to save the image by code similar to the following:

    if (oSession.uriContains("captcha")){      // change as needed
        oSession.utilDecodeResponse();
        var filename = oSession.url;           // get an appropriate file name
        oSession.SaveResponseBody(filename);   // store the file
    }

Now as long as the file being saved by this script can later be found by your program (usually by keying off something in the URL), you're in business.

Community
  • 1
  • 1
yamen
  • 15,390
  • 3
  • 42
  • 52
  • Yes yamen it is related, i was trying to avoid using the proxy, since this object has accessible the captcha height and width i think the string64 or something should be accessable also. – Evan May 27 '12 at 12:45
  • It isn't. The height and width is part of the HTML structure. The actual image is a whole different resource. – yamen May 27 '12 at 13:07
  • Well here's another answer that solves it without using the clipboard if it helps: http://stackoverflow.com/questions/5899417/saving-images-from-a-webbrowser-control – yamen May 27 '12 at 20:47