从网络浏览器复制图像

本文关键字:图像 复制 浏览器 网络 | 更新日期: 2023-09-27 18:25:58

    IHTMLDocument2 doc = (IHTMLDocument2)browser.Document.DomDocument;
    HTMLBody body = (HTMLBody)doc.body;
    Bitmap testImage;
    foreach (IHTMLImgElement img in doc.images)
    {
        if (img.src.IndexOf("some text here", 0) != -1) 
        {
            IHTMLControlRange imgRange;
            imgRange = (IHTMLControlRange)body.createControlRange();
            imgRange.add((IHTMLControlElement)img);
            imgRange.execCommand("Copy", false, null);
            //captchaUrl = img.GetAttribute("src");
            testImage = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
        }
    }

大家好,这是我第三次尝试从 WebBrowser 控件获取图像。首先我尝试了src,这个游戏我形象不好。然后我尝试注入javascript,但没有成功。现在我正在尝试这个。它正在查找图像,但我无法将其转换为位图。

有什么帮助吗?

编辑:代码是 c#

编辑编辑:我只是将有问题的 WebBrowser 控件放在窗体上以查看视觉表示形式,并且我尝试拉取的图像没有加载。也许这就是问题所在?

从网络浏览器复制图像

/*
 * A function to get a Bitmap object directly from a web resource
 * Author: Danny Battison
 * Contact: gabehabe@googlemail.com
 */
/// <summary>
/// Get a bitmap directly from the web
/// </summary>
/// <param name="URL">The URL of the image</param>
/// <returns>A bitmap of the image requested</returns>
public static Bitmap BitmapFromWeb(string URL)
{
    try {
        // create a web request to the url of the image
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
        // set the method to GET to get the image
        myRequest.Method = "GET";
        // get the response from the webpage
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
        // create a bitmap from the stream of the response
        Bitmap bmp = new Bitmap(myResponse.GetResponseStream());
        // close off the stream and the response
        myResponse.Close();
        // return the Bitmap of the image
        return bmp;
    } catch (Exception ex) {
        return null; // if for some reason we couldn't get to image, we return null
    }
}