2011-01-12 16 views
5

是否有可能得到一個控件在winforms中的屏幕截圖,而沒有實際顯示在屏幕上?怎麼樣一個webBrowser組件?通過C#winforms控件的截圖通過C#

+0

我要問 - 這是一個有趣的問題,但爲什麼你需要做到這一點?我真的很好奇,沒有取笑這個問題。 – David 2011-01-12 21:40:17

+0

我需要能夠發送圖形控制儀表的屏幕截圖,只要符合某些條件。 – user441660 2011-01-12 22:05:50

回答

11

是的。這可以做到。

我掀起了一個示例程序來做到這一點。原諒我的命名規則和代碼組織,這個很快就被掀起了。您可以修改它以更好地滿足您的需求,但這顯示了基本知識。

我有三個控件單一形式:

  • 按鈕1:按鈕的默認設置。
  • button2:將Visible屬性設置爲false並將文本設置爲「button2 - 不可見」的按鈕
  • webBrowser1:將可見性設置爲false的WebBrowser控件,將大小設置爲250,101(只是因此它適合。我的形式,當你在捕捉我的回答底部的尺寸是相關的你需要它的大小相應地)

這裏是在Form1的代碼:。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

      PrintInvisibleControl(button2, @"C:\button.jpg"); 
      PrintInvisibleControl(webBrowser1, @"C:\webbrowser.jpg"); 


     } 

     private void PrintInvisibleControl(Control myControl, string filename) 
     { 

      Graphics g = myControl.CreateGraphics(); 
      //new bitmap object to save the image   
      Bitmap bmp = new Bitmap(myControl.Width, myControl.Height); 
      //Drawing control to the bitmap   
      myControl.DrawToBitmap(bmp, new Rectangle(0, 0, myControl.Width, myControl.Height)); 
      bmp.Save(filename, ImageFormat.Jpeg); 
      bmp.Dispose(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      webBrowser1.Navigate("http://www.microsoft.com"); 
     } 

    } 
} 

這導致以下捕獲:

alt text

alt text