Stay Hungry, Stay Foolish

c# 对WebBrowser中指定元素进行截图

C# 7502℃ 0评论

可以对已知id的元素进行截图。

WebBrowser wb_main = new WebBrowser();
string myid="";
private void getPic(string url,string drawid)
{
     myid=drawid;
     wb_main = new WebBrowser();
     wb_main.DocumentCompleted += Wb_main_DocumentCompleted;
     string url = "yourURL";
     wb_main.Navigate(url);
}
private void Wb_main_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     drawPic(myid);
}
private int drawPic(string eleID)
{

     HtmlElement ht = wb_main.Document.GetElementById(eleID);
     if(ht==null)
        return 1;
     //将元素移到最顶层,靠左靠上
     ht.Style = "position: absolute; z-index: 9999; top: 0px; left: 0px";
     var b = new Bitmap(ht.ClientRectangle.Width, ht.ClientRectangle.Height);
     wb_main.Width = ht.ClientRectangle.Width * 2;
     wb_main.Height = ht.ClientRectangle.Height * 2;
     wb_main.DrawToBitmap(b, new Rectangle(new Point(),ht.ClientRectangle.Size));
     pictureBox1.Image = b; //显示到pictureBox1
     return 0;
}
喜欢 (1)
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in C:\inetpub\wordpress\wp-content\themes\XHBlog\comments.php on line 17
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址