以下代码是将网页上的图片保存到剪切板再保存到bincode,能否不经过Clipboard,将IMG传递给bincode,代码应该怎么写?HTMLDocument html = (HTMLDocument)obj.Document.DomDocument;
HTMLBody body = (HTMLBody)html.body;
IHTMLControlRange range = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement IMG;
for (int i = 0; i < obj.Document.Images.Count; i++)
{
IHTMLImgElement img = (IHTMLImgElement)obj.Document.Images[i].DomElement;
if (img.src.IndexOf("Img=") >= 0)
{
IMG = (IHTMLControlElement)obj.Document.Images[i].DomElement;
range.add(IMG);
while (Clipboard.ContainsImage())
Application.DoEvents();
range.execCommand("Copy", false, null);
img = null;
IMG = null;
if (Clipboard.ContainsImage())
{
MemoryStream bincode = new MemoryStream();
Clipboard.GetImage().Save(bincode, System.Drawing.Imaging.ImageFormat.Bmp);
}
break;
}
}
HTMLBody body = (HTMLBody)html.body;
IHTMLControlRange range = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement IMG;
for (int i = 0; i < obj.Document.Images.Count; i++)
{
IHTMLImgElement img = (IHTMLImgElement)obj.Document.Images[i].DomElement;
if (img.src.IndexOf("Img=") >= 0)
{
IMG = (IHTMLControlElement)obj.Document.Images[i].DomElement;
range.add(IMG);
while (Clipboard.ContainsImage())
Application.DoEvents();
range.execCommand("Copy", false, null);
img = null;
IMG = null;
if (Clipboard.ContainsImage())
{
MemoryStream bincode = new MemoryStream();
Clipboard.GetImage().Save(bincode, System.Drawing.Imaging.ImageFormat.Bmp);
}
break;
}
}
是验证码的图片,DOWNLOAD下来的和网页上的不一样的。按我说的代码是可以和网页的一样,但是我不想经过剪切板COPY保存,能否直接将IMG传递给Memorystream
谢谢,按你说的试了,但得到的图片和webBrowser上显示不是同一个验证码图片。WebRequest对img.src发起的请求是获得第二个验证码的图片啊。我希望可以将我一楼的代码中的IMG通过什么办法赋值给bincode而不经过剪切板copy和save的方式才传给bincode。因为我在想如果程序在并发使用时候,很容易造成同时几个程序实例去调用剪切板使用而导致冲突,所以想避免使用剪切板。for (int i = 0; i < webBrowser1.Document.Images.Count; i++)
{
IHTMLImgElement img = (IHTMLImgElement)webBrowser1.Document.Images[i].DomElement;
if (img.src.IndexOf("Img=") >= 0)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(img.src);
WebRequest req = WebRequest.Create(img.src);
WebResponse res = req.GetResponse();
Bitmap bm = new Bitmap(res.GetResponseStream());
pictureBox1.Image = bm;
break;
}
}
private interface IHTMLElementRenderFixed
{
void DrawToDC(IntPtr hdc);
void SetDocumentPrinter(string bstrPrinterName, IntPtr hdc);
}
public Bitmap GetImage(IHTMLImgElement img)
{
IHTMLElementRenderFixed render = (IHTMLElementRenderFixed)img; Bitmap bmp = new Bitmap(e.OffsetRectangle.Width, e.OffsetRectangle.Height); Graphics g = Graphics.FromImage(bmp);
IntPtr hdc = g.GetHdc();
render.DrawToDC(hdc);
g.ReleaseHdc(hdc); return bmp;
}
Bitmap bmp = new Bitmap(e.OffsetRectangle.Width, e.OffsetRectangle.Height);
e是什么?