首先须要添加mshtml的引用,之后using mshtml;
pictureBox1.Image = GetRegCodePic(webBrowser1, "", "code.en.img.php", ""); //第2-4个参数自己填
//第2个参数为网页中图片的name(可能或ID也好用).
//第3个参数为网页中图片的Src.
//第2个参数为网页中图片的Alt.
////例<IMG height=80 alt="Registration Verification Co
pictureBox1.Image.Save("L:\\test\\111.bmp", pictureBox1.Image.RawFormat); //保存图片到本地
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;
}
public static int GetPicIndex(WebBrowser wbMail, string Src, string Alt)
{
int imgnum = -1;
for (int i = 0; i < wbMail.Document.Images.Count; i++) //获取所有的Image元素
{
IHTMLImgElement img = (IHTMLImgElement)wbMail.Document.Images[i].DomElement;
if (Alt == "")
{
if (img.src.Contains(Src)) return i;
}
else
{
if (!string.IsNullOrEmpty(img.alt))
{
if (img.alt.Contains(Alt)) return i;
}
}
}
return imgnum;
}