C# 从WebBrowser复制图像

C# 从WebBrowser复制图像,c#,bitmap,C#,Bitmap,大家好,这是我第三次尝试从WebBrowser控件拍摄图像。首先我尝试了src,这个游戏给我一个坏印象。然后我尝试注入javascript,但没有成功。现在我正在尝试这个。它正在查找图像,但我无法将其转换为位图 有什么帮助吗 编辑:代码是c# 编辑:我只是将有问题的WebBrowser控件放在表单上,以查看视觉表示,而我尝试拉取的图像没有加载。也许这就是问题所在?/* IHTMLDocument2 doc = (IHTMLDocument2)browser.Document.DomDoc

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

有什么帮助吗

编辑:代码是c#

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

/*
    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);
        }
    }
*直接从web资源获取位图对象的函数 *作者:丹尼·巴蒂森 *联系人:gabehabe@googlemail.com */ /// ///直接从web获取位图 /// ///图像的URL ///请求的图像的位图 公共静态位图BitmapFromWeb(字符串URL) { 试一试{ //创建对图像url的web请求 HttpWebRequest myRequest=(HttpWebRequest)WebRequest.Create(URL); //设置获取图像的方法 myRequest.Method=“GET”; //从网页中获取响应 HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse(); //从响应流创建位图 位图bmp=新位图(myResponse.GetResponseStream()); //关闭流和响应 myResponse.Close(); //返回图像的位图 返回bmp; }捕获(例外情况除外){ return null;//如果由于某种原因无法访问映像,则返回null } }
它是哪种语言?请正确标记它。可能的副本您必须使用WebBrowser控件,还是您的要求是保存web上的图像?您可以使用HttpWebRequest,我试过了,但不起作用。我已经找了三个多小时了。HTTPWebRequest只会使它更加复杂,WebBrowser允许您访问控件。HTTPWebRequest只是html,我不同意。请看我的答案。我已经在问题中说明,我已经尝试通过URL获取图像,这对我的情况不起作用。我需要在web浏览器控件中显示图像。
/*
 * 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
    }
}