elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  [Aporte] Captura de Pantalla en C#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Aporte] Captura de Pantalla en C#  (Leído 1,781 veces)
samuelhm

Desconectado Desconectado

Mensajes: 28



Ver Perfil
[Aporte] Captura de Pantalla en C#
« en: 30 Mayo 2014, 13:01 pm »

Me costó encontrar la manera de hacer una captura de una ventana en c#, en c++ estaba apuntado en un post con chincheta,pero en c# no encontré nada, por si alguien alguna vez busca el modo, asi es como lo he podido hacer:

MetodosNativos.CS
Código
  1. public static class MetodosNativos
  2.    {
  3. public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
  4.  
  5.        [DllImport("gdi32.dll")]
  6.        public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
  7.            int nWidth, int nHeight, IntPtr hObjectSource,
  8.            int nXSrc, int nYSrc, int dwRop);
  9.  
  10.        [DllImport("gdi32.dll")]
  11.        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,int nHeight);
  12.  
  13.        [DllImport("gdi32.dll")]
  14.        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  15.  
  16.        [DllImport("gdi32.dll")]
  17.        public static extern bool DeleteDC(IntPtr hDC);
  18.  
  19.        [DllImport("gdi32.dll")]
  20.        public static extern bool DeleteObject(IntPtr hObject);
  21.  
  22.        [DllImport("gdi32.dll")]
  23.        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  24.  
  25.  
  26.        /*User32.Dll*/
  27.        [StructLayout(LayoutKind.Sequential)]
  28.        public struct RECT
  29.        {
  30.            public int left;
  31.            public int top;
  32.            public int right;
  33.            public int bottom;
  34.        }
  35.        public enum GetWindow_Cmd : uint
  36.        {
  37.            GW_HWNDFIRST = 0,
  38.            GW_HWNDLAST = 1,
  39.            GW_HWNDNEXT = 2,
  40.            GW_HWNDPREV = 3,
  41.            GW_OWNER = 4,
  42.            GW_CHILD = 5,
  43.            GW_ENABLEDPOPUP = 6
  44.        }
  45.        [DllImport("user32.dll")]
  46.        public static extern IntPtr GetDesktopWindow();
  47.  
  48.        [DllImport("user32.dll")]
  49.        public static extern IntPtr GetWindowDC(IntPtr hWnd);
  50.  
  51.        [DllImport("user32.dll")]
  52.        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
  53.  
  54.        [DllImport("user32.dll")]
  55.        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
  56. }
  57.  
Capturadora.cs
Código
  1. <....Codigo de la clase
  2. public static Bitmap CapturarVentana(IntPtr handle)
  3.        {
  4.            try
  5.            {
  6.                RedimensionarVentana(handle);
  7.                // get te hDC of the target window
  8.                IntPtr hdcSrc = MetodosNativos.GetWindowDC(handle);
  9.                // get the size
  10.                MetodosNativos.RECT windowRect = new MetodosNativos.RECT();
  11.                MetodosNativos.GetWindowRect(handle, ref windowRect);
  12.                int width = windowRect.right - windowRect.left;
  13.                int height = windowRect.bottom - windowRect.top;
  14.                // create a device context we can copy to
  15.                IntPtr hdcDest = MetodosNativos.CreateCompatibleDC(hdcSrc);
  16.                // create a bitmap we can copy it to,
  17.                // using GetDeviceCaps to get the width/height
  18.                IntPtr hBitmap = MetodosNativos.CreateCompatibleBitmap(hdcSrc, width, height);
  19.                // select the bitmap object
  20.                IntPtr hOld = MetodosNativos.SelectObject(hdcDest, hBitmap);
  21.                // bitblt over
  22.                MetodosNativos.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, MetodosNativos.SRCCOPY);
  23.                // restore selection
  24.                MetodosNativos.SelectObject(hdcDest, hOld);
  25.                // clean up
  26.                MetodosNativos.DeleteDC(hdcDest);
  27.                MetodosNativos.ReleaseDC(handle, hdcSrc);
  28.                // get a .NET image object for it
  29.                Bitmap bmp = Bitmap.FromHbitmap(hBitmap);
  30.                MetodosNativos.DeleteObject(hBitmap);
  31.                return bmp;
  32.            }
  33.            catch (Exception err) {MessageBox.show(err.ToString()); return null; }
  34.  
  35.        }
  36. ....Codigo...->
  37.  

El metodo retorna un Bitmap de la ventana que le digas como parametro, aunque tambien se podria hacer sin retorno y hacer bmp.save("ruta\archivo.bmo"); si lo que buscas es crear un archivo con la captura.


« Última modificación: 30 Mayo 2014, 13:03 pm por samuelhm » En línea

samuelhm

Desconectado Desconectado

Mensajes: 28



Ver Perfil
Re: [Aporte] Captura de Pantalla en C#
« Respuesta #1 en: 30 Mayo 2014, 13:13 pm »

Otra cosa que no podia hacer en c#, era conseguir Expander/Contraer todos los elementos, como por ejemplo los bucles.
Acostumbrado a visual c++ me era super incomodo, pero lo conseguí con este paquete de expansión:
http://visualstudiogallery.msdn.microsoft.com/6c3c5dec-1534-4c42-81b1-cfd4615fd0e9


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Captura de Pantalla « 1 2 »
Programación Visual Basic
kakinets 15 5,724 Último mensaje 6 Octubre 2005, 22:32 pm
por NYlOn
Ayuda Captura de Pantalla!!!!!
Programación Visual Basic
Badlands 3 1,708 Último mensaje 29 Agosto 2006, 02:49 am
por maxnet
Captura de Pantalla
.NET (C#, VB.NET, ASP)
David Vans 2 2,811 Último mensaje 24 Mayo 2008, 14:10 pm
por David Vans
Clase que Captura el Inicio y Cierre de Procesos [Aporte] [WMI]
.NET (C#, VB.NET, ASP)
Keyen Night 0 1,884 Último mensaje 28 Septiembre 2011, 22:20 pm
por Keyen Night
Como agrandar una captura de pantalla sin pixelarla?
Windows
DonPilin 3 3,354 Último mensaje 13 Abril 2022, 17:25 pm
por el-brujo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines