【问题标题】:Copy an image from WebBrowser从 WebBrowser 复制图像
【发布时间】:2012-02-28 13:56:03
【问题描述】:
    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 控件放在表单上以查看视觉表示,而我试图拉出的图像没有加载。也许这就是问题所在?

【问题讨论】:

  • 是什么语言?请正确标记它。
  • 您是否使用 WebBrowser 控件,或者您的要求是保存网络上的图像?您可能会使用 HttpWebRequest
  • 我试过了,但它不起作用。我已经找了3个多小时了。 HTTPWebRequest 只会使它更复杂,WebBrowser 允许您访问控件。 HTTPWebRequest 只是 html。
  • 好吧,我不同意。看我的回答。

标签: c# bitmap


【解决方案1】:
/*
 * 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
    }
}

【讨论】:

  • 我已经在问题中说明我试图通过 URL 获取图像,这不适用于我的情况。我需要获取 Web 浏览器控件中显示的图像。
猜你喜欢
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多