2008-11-05 58 views

回答

48

每個控件都有一個叫做DrawToBitmap的方法。你不需要p/invoke來做到這一點。

Control c = new TextBox(); 
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(c.Width, c.Height); 
c.DrawToBitmap(bmp, c.ClientRectangle); 
3

對於支持它的WinForms控制,存在System.Windows.Forms.Control類的方法:

public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds); 

這並不適用於所有的控制工作。然而,。第三方組件供應商有更全面的解決方案。

7

你可以得到一個.NET控制的圖像編程很容易地使用控制類的DrawToBitmap方法開始在.NET 2.0

這裏是在VB

Dim formImage As New Bitmap("C:\File.bmp") 
    Me.DrawToBitmap(formImage, Me.Bounds) 
樣品

這裏,它是在C#:

Bitmap formImage = New Bitmap("C:\File.bmp") 
this.DrawToBitmap(formImage, this.Bounds) 
1

如果不是對合當你想要做的時候,你通常可以將它投射到基本控件類並在那裏調用DrawToBitmap方法。

5

Control.DrawToBitmap可讓您將大多數控件繪製到位圖上。這不適用於RichTextBox和其他人。如果你想捕獲這些,或者擁有其中一個的控件,那麼你需要像Jeff所建議的代碼項目文章http://www.codeproject.com/KB/graphics/imagecapture.aspx中所描述的那樣進行PInvoke。注意這些方法中的一些會捕獲屏幕上的任何內容,所以如果你有另一個窗口覆蓋你的控件,你會得到它。

1
Panel1.Dock = DockStyle.None ' If Panel Dockstyle is in Fill mode 
Panel1.Width = 5000 ' Original Size without scrollbar 
Panel1.Height = 5000 ' Original Size without scrollbar 

Dim bmp As New Bitmap(Me.Panel1.Width, Me.Panel1.Height) 
Me.Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Panel1.Width, Me.Panel1.Height)) 
'Me.Panel1.DrawToBitmap(bmp, Panel1.ClientRectangle) 
bmp.Save("C:\panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 

Panel1.Dock = DockStyle.Fill 

注:它的做工精細

2

這是如何做到這一點對整個Form,而不僅僅是客戶端區域(沒有標題欄和其他敷料)

 Rectangle r = this.Bounds; 
     r.Offset(-r.X,-r.Y); 
     Bitmap bitmap = new Bitmap(r.Width,r.Height); 
     this.DrawToBitmap(bitmap, r); 
     Clipboard.SetImage(bitmap); 
相關問題