Webbrowser控件如何获取验证码图片?是一个图像对象

How to get verification code image from Webbrowser control? Be an Image object

验证码图片。 所以请不要回答下载 url.

由于一些错误,"copy commmand"有时只是复制url,而不是图像。 所以请不要使用 IHTMLControlRange.execCommand 方法回答。

有些网站不是 HTML5 页面。 所以请不要回答通过 canvas 标签获取它。

..... 还有其他方法吗?

好吧,如果您使用的是 Winforms,则可以使用图形对象截取 WebBroser 控件中呈现的所有内容的屏幕截图。

以下是您可以执行此操作的方法:

   private void CaptureImage()
    {


        int width, height;
        width = webBrowser1.ClientRectangle.Width;
        height = webBrowser1.ClientRectangle.Height;
        using (Bitmap image = new Bitmap(width, height))
        {
            using (Graphics graphics = Graphics.FromImage(image))
            {
                Point p, upperLeftSource, upperLeftDestination;
                p = new Point(0, 0);
                upperLeftSource = webBrowser1.PointToScreen(p);
                upperLeftDestination = new Point(0, 0);
                Size blockRegionSize = webBrowser1.ClientRectangle.Size;
                graphics.CopyFromScreen(upperLeftSource, upperLeftDestination, blockRegionSize);
            }
            //saveout the image
            var path = System.IO.Path.Combine(System.Environment.CurrentDirectory, "image.png");
            image.Save(path, System.Drawing.Imaging.ImageFormat.Png);

        }
    }

我们很清楚,Bitmap是System.Drawing.Image的具体实现。

好的...我正在尝试下载...它有效!

只是在浏览器控件中使用与页面相同的参数伪造请求。

var hRequest = WebRequest.CreateHttp("/*Rand Code Url*/?rnd=" + new Random().NextDouble());
hRequest.Accept = "image/png, image/svg+xml, image/jxr, image/*; q=0.8, */*; q=0.5";
hRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
hRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, /*Page AcceptLanguage*/);
hRequest.KeepAlive = true;
string cookie = "";
webBrowser.Invoke((MethodInvoker)delegate { cookie = webBrowser.Document.Cookie; });
hRequest.Headers.Add(HttpRequestHeader.Cookie, cookie);
hRequest.Headers.Add("DNT", "1");
hRequest.Host = /*page window.location.host*/;
hRequest.Referer = /*page.referer*/;
hRequest.UserAgent = /*page navigator.userAgent*/;
hRequest.Method = "GET";
var hResponse = hRequest.GetResponse();
var response = hResponse.GetResponseStream();
List<byte> data = new List<byte>();
//read stream
while (true)
{
    int i = response.ReadByte();
    if (i >= 0)
        data.Add((byte)i);
    else
        break;
}
if (data.Count == 0) return "";//if not succes, return empty string
//convert to Base64 string
string RandBase64 = Convert.ToBase64String(data.ToArray());
hResponse.Dispose();