Cómo obtener una captura de pantalla de a.Net ¿Control de WinForms mediante programación?


¿Cómo se obtiene mediante programación una imagen de un control. Net?

Author: Alex Wiese, 2008-11-05

7 answers

Hay un método en cada control llamado DrawToBitmap. No es necesario que p/invoke para hacer esto.

Control c = new TextBox();
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(c.Width, c.Height);
c.DrawToBitmap(bmp, c.ClientRectangle);
 48
Author: Will,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-05 18:27:34

Puede obtener una imagen de un control. NET programáticamente fácilmente usando el DrawToBitmap método de la clase de control que comienza en. NET 2.0

Aquí hay una muestra en VB

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

Y aquí está en C#:

 Bitmap formImage = New Bitmap("C:\File.bmp")
 this.DrawToBitmap(formImage, this.Bounds)
 7
Author: Joey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-09 09:56:13

Control.DrawToBitmap le permitirá dibujar la mayoría de los controles en un mapa de bits. Esto no funciona con RichTextBox y algunos otros. Si desea capturar estos, o un control que tiene uno de ellos, entonces necesita hacer PInvoke como se describe en el artículo code project http://www.codeproject.com/KB/graphics/imagecapture.aspx , sugerido por Jeff. Tenga cuidado de que algunos de estos métodos capturarán lo que esté en la pantalla, por lo que si tiene otra ventana que cubre su control, obtendrá eso en su lugar.

 5
Author: Hallgrim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-05 20:45:02

Para los controles WinForms que lo soportan, hay un método en el Sistema.Windows.Forma.Clase de control:

public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds);

Esto no funciona con todos los controles, sin embargo. Los proveedores de componentes de terceros tienen soluciones más completas.

 3
Author: Alan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-05 18:28:09

Esta es la forma de hacerlo para todo Form, no solo el área del Cliente (que no tiene la barra de título y otros vestidores)

        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);
 2
Author: Mark Lakata,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-06 23:25:39

Si no está en el control que está tratando de hacer, generalmente puede enviarlo a la clase de control base y llamar al método DrawToBitmap allí.

 1
Author: Nick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-05 18:36:28
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

Nota: Está funcionando bien

 1
Author: R Muruganandhan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-09 21:40:01