C# 如何以编程方式获取 .Net WinForms 控件的屏幕截图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/266116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:31:50  来源:igfitidea点击:

How to get a screen capture of a .Net WinForms control programmatically?

c#.netimagewinformscapture

提问by user34787

How do you programmatically obtain a picture of a .Net control?

如何以编程方式获取 .Net 控件的图片?

采纳答案by user34787

There's a method on every control called DrawToBitmap. You don't need to p/invoke to do this.

每个控件上都有一个名为DrawToBitmap 的方法。您不需要 p/invoke 来执行此操作。

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

回答by Alan

For WinForms controls that support it, there is a method in the System.Windows.Forms.Control class:

对于支持它的 WinForms 控件,System.Windows.Forms.Control 类中有一个方法:

public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds);

This does not work with all controls, however. Third party component vendors have more comprehensive solutions.

但是,这不适用于所有控件。第三方组件供应商有更全面的解决方案。

回答by Joey

You can get a picture of a .NET control programmatically pretty easily using the DrawToBitmapmethod of the Control class starting in .NET 2.0

从 .NET 2.0 开始,您可以使用Control 类的DrawToBitmap方法以编程方式轻松获取 .NET 控件的图片

Here is a sample in VB

这是VB中的示例

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

And here it is in C#:

这是在 C# 中的:

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

回答by Nick

if it's not on the control you're trying to do, you can usually cast it to the base Control class and call the DrawToBitmap method there.

如果它不在您尝试执行的控件上,您通常可以将其转换为基本 Control 类并在那里调用 DrawToBitmap 方法。

回答by Hallgrim

Control.DrawToBitmapwill let you draw most controls to a bitmap. This does not work with RichTextBox and some others.

Control.DrawToBitmap将让您将大多数控件绘制到位图。这不适用于 RichTextBox 和其他一些。

If you want to capture these, or a control that has one of them, then you need to do PInvoke like described in this CodeProject article: Image Capture

如果您想捕获这些或具有其中之一的控件,那么您需要像这篇 CodeProject 文章中描述的那样执行 PInvoke:Image Capture

Take care that some of these methods will capture whatever is on the screen, so if you have another window covering your control you will get that instead.

请注意,其中一些方法将捕获屏幕上的任何内容,因此如果您有另一个窗口覆盖您的控件,您将获得它。

回答by R Muruganandhan

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

Note:Its working fine

注意:它工作正常

回答by Mark Lakata

This is how to do it for an entire Form, not just the Client area (which doesn't have the title bar and other dressing)

这是整个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);