2008-11-05 184 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); 
相关问题