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控件置于表单上以查看可视化表示,而我试图拉动的图像未加载。也许这就是问题?
答案 0 :(得分:0)
/*
* 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
}
}