//取得驗證碼圖片 public Image GetRegCodePic(WebBrowser WB, String ImgName, String Src, String Id) { HTMLDocument doc = WB.Document.DomDocument as HTMLDocument; HTMLBody body = doc.body as HTMLBody; IHTMLControlRange rang = body.createControlRange() as IHTMLControlRange; IHTMLControlElement Img; if (ImgName == "") { //如果沒有圖片的名字,通過Src或Alt中的關鍵字來取 Int32 ImgNum = GetPicIndex(WB, Src, Id); if (ImgNum == -1) { return(null); } Img = WB.Document.Images[ImgNum].DomElement as IHTMLControlElement; } else { Img = WB.Document.All[ImgName].DomElement as IHTMLControlElement; } rang.add(Img); rang.execCommand("copy", false, Type.Missing); Image RegImg = Clipboard.GetImage(); Clipboard.Clear(); return(RegImg); }
/// <summary> /// 获取WebBrowser指定的图片 /// </summary> /// <param name="webBrowser">需要获取图片的WebBrowser</param> /// <param name="imgID">指定的图片的id(优先查找指定id)</param> /// <param name="imgSrc">指定的图片的src,支持模糊查询</param> /// <param name="imgAlt">指定的图片的src, 支持模糊查询</param> /// <returns></returns> public static Image GetRegCodePic(ref WebBrowser webBrowser, String imgID, String imgSrc, String imgAlt) { HTMLDocument doc = (HTMLDocument)webBrowser.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement img; // 如果没有图片的ID,通过Src或Alt中的关键字来取 if (imgID.Length == 0) { Int32 ImgNum = GetPicIndex(ref webBrowser, ref imgSrc, ref imgAlt); if (ImgNum == -1) { return(null); } img = (IHTMLControlElement)webBrowser.Document.Images[ImgNum].DomElement; } else { img = (IHTMLControlElement)webBrowser.Document.All[imgID].DomElement; } rang.add(img); rang.execCommand("Copy", false, null); Image regImg = Clipboard.GetImage(); Clipboard.Clear(); return(regImg); }
/// <summary> /// 返回指定WebBrowser中图片<IMG></IMG>中的图内容 /// </summary> /// <param name="WebCtl">WebBrowser控件</param> /// <param name="ImgeTag">IMG元素</param> /// <returns>IMG对象</returns> public static Image GetWebImage(Form1 form, WebBrowser WebCtl, HtmlElement ImgeTag) { Image image = null; form.Invoke((EventHandler)(delegate { // 这里写你的控件代码,比如 HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址 Image oldImage = Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 image = Clipboard.GetImage(); try { Clipboard.SetImage(oldImage); } catch { } } )); return(image); }
private Image GetWebImage(HTMLDocument doc, IHTMLControlElement ImgeTag) { HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = ImgeTag; //图片地址 Image oldImage = Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); try { if (oldImage != null) { Clipboard.SetImage(oldImage); } } catch (Exception ex) { // MessageBox.Show("GetWebImage:" + ex.Message); } return(numImage); }
/* * * 示例1: * 下面是某个站的注册码图片的HTML部分源代码 * <IMG height=80 alt="Registration Verification Code" src="......" width=290 border=0> * picturebox1.Image =GetRegCodePic(wbMail, "", "", "Registration Verification Code") * * 示例2: * 下面是某个站的注册码图片的HTML部分源代码 * <IMG id=CAPTCHAImage src="......." name=CAPTCHAImage> * picturebox1.Image =GetRegCodePic(wbMail, "CAPTCHAImage", "", "") //通过验证码Html元素的名字来 * */ #endregion /// <summary> /// 获取验证码图片 /// </summary> /// <param name="wbMail"></param> /// <param name="ImgName"></param> /// <param name="Src"></param> /// <param name="Alt"></param> /// <returns></returns> public static Image GetRegCodePic(WebBrowser wbMail, string ImgName, string Src, string Alt) { HTMLDocument doc = (HTMLDocument)wbMail.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img; if (ImgName == "") //如果没有图片的名字,通过Src或Alt中的关键字来取 { int ImgNum = GetPicIndex(wbMail, Src, Alt); if (ImgNum == -1) { return(null); } Img = (IHTMLControlElement)wbMail.Document.Images[ImgNum].DomElement; } else { Img = (IHTMLControlElement)wbMail.Document.All[ImgName].DomElement; } rang.add(Img); rang.execCommand("Copy", false, null); Image RegImg = Clipboard.GetImage(); Clipboard.Clear(); return(RegImg); }
private async Task <ImageSource> GetVerifyCodeInternal() { //await HookScript(WebBrowser, "/Assets/Json.js"); //await HookScript(WebBrowser, "/Assets/GetCode.js"); //var resultData = WebBrowser.Document.InvokeScript("GetCode"); //var result = JsonConvert.DeserializeObject<ImageSize>(resultData.ToString()); //if (result.ErrorCode != ErrorCode.Ok) //{ // throw new Exception($"{result.ErrorCode}"); //} foreach (HtmlElement image in WebBrowser.Document.Images) { var dom = (IHTMLImgElement)image.DomElement; if (dom.src == "http://www.tsdm.net/plugin.php?id=oracle:verify") { HTMLBody body = (HTMLBody)WebBrowser.Document.Body.DomElement; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); rang.add((IHTMLControlElement)dom); var result = rang.execCommand("Copy"); await Task.Delay(1000); Bitmap bitmap = (Bitmap)System.Windows.Forms.Clipboard.GetImage(); var s = BitmapToBitmapSource(bitmap); return(s); } } return(null); }
private Image GetCheckImage(HtmlElement ImageTag) { HTMLDocument doc = (HTMLDocument)webBrowser1.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImageTag.DomElement; //圖片地址 bool containImg = Clipboard.ContainsImage(); bool containText = Clipboard.ContainsText(); Image oldImage = Clipboard.GetImage(); string oldText = Clipboard.GetText(); rang.add(Img); rang.execCommand("Copy", false, null); //拷貝到內存 Image numImage = Clipboard.GetImage(); //從Clipboard中取圖 if (containImg) { try { Clipboard.SetImage(oldImage); } //還原 catch { } } else if (containText) { try { Clipboard.SetText(oldText); } //還原 catch { } } return(numImage); }
/// <summary> /// 获取验证码图片 /// </summary> /// <param name="imgId"></param> /// <returns></returns> private Image GetValidateCode(string imgId) { Image myImage = new Bitmap(60, 20); HtmlDocument doc = webBrowser1.Document; IHTMLControlElement img = (IHTMLControlElement)doc.Images[imgId].DomElement; IHTMLDocument2 tmpDoc = webBrowser1.Document.DomDocument as IHTMLDocument2; HTMLBody body = (HTMLBody)tmpDoc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); rang.add(img); rang.execCommand("Copy", false, null); myImage = Clipboard.GetImage(); return(myImage); }
/// <summary> /// 获取图像内容 /// </summary> /// <param name="browser">显示图像的浏览器控件</param> /// <param name="imgElement">从浏览器中读取到的img</param> private static Image GetImage(WebBrowser browser, HtmlElement imgElement) { HTMLDocument doc = (HTMLDocument)browser.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement img = (IHTMLControlElement)(imgElement.DomElement); rang.add(img); rang.execCommand("Copy", false, null); Image regImg = Clipboard.GetImage(); Clipboard.Clear(); return(regImg); }
/// <summary> /// 返回指定WebBrowser中图片<IMG></IMG>中的图内容 /// </summary> /// <param name="webCtl">WebBrowser控件</param> /// <param name="imgeTag">IMG元素</param> /// <returns>IMG对象</returns> private Image GetWebImage(WebBrowser webBrowser, string imgeTagId) { System.Windows.Forms.HtmlDocument winHtmlDoc = webBrowser.Document; HTMLDocument doc = (HTMLDocument)winHtmlDoc.DomDocument; HtmlElement imgTag = winHtmlDoc.GetElementById(imgeTagId); HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement imgE = (IHTMLControlElement)imgTag.DomElement; //图片地址 rang.add(imgE); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); return(numImage); }
/// <summary> /// 返回指定WebBrowser中图片<IMG></IMG>中的图内容 /// </summary> /// <param name="WebCtl">WebBrowser控件</param> /// <param name="ImgeTag">IMG元素</param> /// <returns>IMG对象</returns> private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag) { HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址 Image oldImage = Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); Clipboard.SetImage(oldImage); return(numImage); }
/// <summary> /// 得到验证码图片 /// </summary> /// <param name="doc"></param> /// <param name="ImgName"></param> /// <returns></returns> public static Image GetPic(HtmlDocument doc, string ImgName) { Image RegImg; HTMLDocument domDoc = (HTMLDocument)doc.DomDocument; HTMLBody body = (HTMLBody)domDoc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img; Img = (IHTMLControlElement)doc.All[ImgName].DomElement; rang.add(Img); rang.execCommand("Copy", false, null); RegImg = Clipboard.GetImage(); Clipboard.Clear(); return(RegImg); }
public void GetCacheImage(WebBrowser webBrower, string id, MyDelegate myDelegate, Window window) { HTMLDocument doc = (HTMLDocument)webBrower.Document; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement img = (IHTMLControlElement)(doc.getElementById(id)); rang.add(img); rang.execCommand("Copy", true, null); try { BitmapSource bitmap = Clipboard.GetImage(); byte[] dcimg = ConvertToBytes(bitmap);//验证码字节码 Clipboard.Clear(); string result = Dc.RecByte_A(dcimg, dcimg.Length, ConfigurationManager.AppSettings["chaorenuser"], ConfigurationManager.AppSettings["chaorenpw"], ConfigurationManager.AppSettings["softid"]); string verificationCode = result.Substring(0, result.IndexOf("|!|")); myDelegate(verificationCode); doc = null; body = null; rang = null; img = null; GC.Collect(); } catch (Exception e) { Thread thr = new Thread(() => { string userSaveFiler = Directory.GetCurrentDirectory() + "\\weeorlog.txt";//用户账号保存文件 FileStream fs = new FileStream(userSaveFiler, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(e.ToString()); sw.Close(); fs.Close(); //这里还可以处理些比较耗时的事情。 Thread.Sleep(1000);//延时2秒 window.Dispatcher.Invoke(new Action(() => { Clipboard.Clear(); GetCacheImage(webBrower, id, myDelegate, window); })); }); thr.Start(); } }
//讀入驗證碼 private Image Getimage() { HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument; HTMLBody body = doc.body as HTMLBody; IHTMLControlRange range = body.createControlRange(); //取得網頁中第[0]個圖片(因為我要取得的圖片的沒有ID) //也可使用GetElementById("ID名稱")取得圖片 IHTMLControlElement imgElement = webBrowser1.Document.GetElementsByTagName("img")[0].DomElement as IHTMLControlElement; //複製圖片至剪貼簿 range.add(imgElement); range.execCommand("copy", false, Type.Missing); //取得圖片 img = Clipboard.GetImage(); Clipboard.Clear(); return(img); }
public Image GetWebImage(HTMLBody body, HtmlElement ImageTag) { IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImageTag.DomElement; //图片地址 Image oldImage = Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); try { Clipboard.SetImage(oldImage); } catch { } return numImage; }
public static Image GetRegCodePic(WebBrowser wb, HtmlElement ImgeTag) { if (ImgeTag.TagName.ToUpper() != "IMG") { return(null); } HTMLDocument doc = (HTMLDocument)wb.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址 Image oldImage = Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); Clipboard.Clear(); return(numImage); }
/// <summary> /// 获取页面验证码图片转换为字符串 /// </summary> /// <param name="webbrowser"></param> /// <returns></returns> public string ConImageCodeToString(System.Windows.Forms.WebBrowser webbrowser, out HtmlElement imagecodeelement) { string imagecode = default(string); try { #region 解析验证码 HtmlElementCollection imageresult = webbrowser.Document.Images; imagecodeelement = null; foreach (var item in imageresult) { HtmlElement curimg = item as HtmlElement; if (curimg.OuterHtml.Contains("rand_code.csp")) { /*获取页面图片*/ imagecodeelement = curimg; HTMLDocument hdoc = (HTMLDocument)webbrowser.Document.DomDocument; HTMLBody hbody = (HTMLBody)hdoc.body; IHTMLControlRange hcr = (IHTMLControlRange)hbody.createControlRange(); IHTMLControlElement hImg = (IHTMLControlElement)curimg.DomElement; hcr.add(hImg); hcr.execCommand("Copy", false, null); Image CodeImage = Clipboard.GetImage(); Bitmap bitmap = new Bitmap(CodeImage); /*******解析验证码********/ BreakCodeServer unCheckobj = new BreakCodeServer(bitmap); Thread.Sleep(2000); imagecode = unCheckobj.getPicnum(); break; } } #endregion } catch (Exception ex) { SetText(System.DateTime.Now.ToString() + "\r\n解析验证码失败\r\n"); imagecodeelement = null; imagecode = ""; } return(imagecode); }
private System.Drawing.Image GetWebImage(System.Windows.Forms.WebBrowser WebCtl, HtmlElement ImgeTag) { HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址 var oldImage = System.Windows.Forms.Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 var numImage = System.Windows.Forms.Clipboard.GetImage(); try { System.Windows.Forms.Clipboard.SetImage(oldImage); } catch { } return(numImage); }
public static Image GetWebImage_test(WebBrowser WebCtl, string doucmentName) { HtmlElement ImgeTag = WebCtl.Document.All[doucmentName]; HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址 Image oldImage = Clipboard.GetImage(); rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); try { Clipboard.SetImage(oldImage); } catch { } return(numImage); }
public override object getVerCodeImage(HtmlDocument indoc) { HTMLDocument doc = (HTMLDocument)indoc.DomDocument; if (doc == null) { return(doc); } //Image img = null; HtmlElement ImgeTag = indoc.GetElementById("img-valcode"); //mshtml.IHTMLElementCollection HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址 object oldobj = Clipboard.GetDataObject(); //备份粘贴版数据 rang.add(Img); rang.execCommand("Copy", false, null); //拷贝到内存 Image numImage = Clipboard.GetImage(); rang = null; return(numImage); }
public string getVimg(HtmlDocument document, string vimgID) { HtmlElement vimg = document.GetElementById(vimgID); HTMLDocument doc = (HTMLDocument)document.DomDocument; HTMLBody body = (HTMLBody)doc.body; IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange(); IHTMLControlElement img = (IHTMLControlElement)vimg.DomElement; try { Image oldImage = Clipboard.GetImage(); rang.add(img); rang.execCommand("Copy", false, null); Image numImage = Clipboard.GetImage(); Clipboard.SetImage(oldImage); using (Page page = engine.Process(new Bitmap(numImage))) { return(page.GetText()); } }catch (Exception ex) { return(""); } }
private void btn_fillone_Click(object sender, EventArgs e) { if ("http://cwpt.suda.edu.cn/WFManager/login.jsp" != webBrowser1.Url.ToString()) { MessageBox.Show("不正确的页面, 不要乱用功能!"); } else if (username == "" || password == "") { MessageBox.Show("用户名或密码不完整!"); return; } else { //根据验证码ID获取验证码元素 HtmlElement ImageCodeTag = webBrowser1.Document.GetElementById("checkcodeImg"); //获取网页所有内容 HTMLDocument hdoc = (HTMLDocument)webBrowser1.Document.DomDocument; //获取网页body标签中的内容 HTMLBody hbody = (HTMLBody)hdoc.body; //创建一个接口 IHTMLControlRange hcr = (IHTMLControlRange)hbody.createControlRange(); //获取图片地址 IHTMLControlElement hImg = (IHTMLControlElement)ImageCodeTag.DomElement; //将图片添加到接口中 hcr.add(hImg); //将图片复制到内存 hcr.execCommand("Copy", false, null); //从粘贴板得到图片 Image CodeImage = Clipboard.GetImage(); //返回得到的验证码 //CodeImage.Save("123.png"); // 识别验证码 byte[] Buffer = ImageToBytes(CodeImage); StringBuilder Result = new StringBuilder('\0', 256); string coderesult = ""; if (GetImageFromBuffer(Buffer, Buffer.Length, Result)) { coderesult = Result.ToString(); } else { coderesult = "识别失败"; } //填充用户名密码验证码 HtmlElement name = webBrowser1.Document.GetElementById("uid"); if (name != null) { name.SetAttribute("value", username); } HtmlElement pass = webBrowser1.Document.GetElementById("pwd"); if (pass != null) { pass.SetAttribute("value", password); } HtmlElement code = webBrowser1.Document.GetElementById("chkcode"); if (code != null) { code.SetAttribute("value", coderesult); } HtmlElement loginbtn = webBrowser1.Document.GetElementById("loginbtn2"); if (loginbtn != null) { loginbtn.InvokeMember("click"); } } }