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

 

 


Tema destacado: Estamos en la red social de Mastodon


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

Desconectado Desconectado

Mensajes: 167



Ver Perfil
API PrintWindow
« en: 17 Julio 2011, 14:39 pm »

Hola,
Veréis quiero capturar con cierta frecuencia una ventana. Según he podido encontrar este sería el código que se usaría. Funcionar me funciona, el problema está en que cuando lleva varios minutos, la pantalla que está capturando empieza a verse mal. Alguna idea para mejorar esto y que la otra ventana se vea bien?

Código:
        public static Bitmap GetWindow(IntPtr hWnd)
        {
            Bitmap bmp = null;
            IntPtr hdc = GetDC(hWnd);
            if ((int)hdc != 0)
            {
                IntPtr hdcMem = CreateCompatibleDC(hdc);
                if ((int)hdcMem != 0)
                {
                    IntPtr hbitmap = CreateCompatibleBitmap(hdc, GetSystemMetrics(0), GetSystemMetrics(1));
                    if ((int)hbitmap != 0)
                    {
                        SelectObject(hdcMem, hbitmap);
                        if (PrintWindow(hWnd, hdcMem, 1))
                        {
                            bmp = System.Drawing.Image.FromHbitmap(hbitmap);
                        }
                    }
                    DeleteObject(hbitmap);
                }
                    DeleteObject(hdcMem);
            }
            ReleaseDC(hWnd, hdc);
            return bmp;
        }

Muchas gracias.


En línea

seba123neo


Desconectado Desconectado

Mensajes: 3.621



Ver Perfil WWW
Re: API PrintWindow
« Respuesta #1 en: 17 Julio 2011, 22:33 pm »

Hola, proba esto, lo he probado con algunas ventanas y funciona.

Código
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.    <DllImport("User32.dll", SetLastError:=True)> _
  5.    Private Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hDC As IntPtr, ByVal nFlags As UInteger) As Boolean
  6.    End Function
  7.  
  8.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.        Me.BackgroundImage = CapturarPantalla(&H101A2)
  10.    End Sub
  11.  
  12.    Private Function CapturarPantalla(ByVal pHandle As Integer) As Bitmap
  13.        Dim g As Graphics = Me.CreateGraphics
  14.        Dim bmp As New Bitmap(Me.Size.Width, Me.Size.Height, g)
  15.        Dim memoryGraphics As Graphics = Graphics.FromImage(bmp)
  16.        Dim dc As IntPtr = memoryGraphics.GetHdc()
  17.  
  18.        Dim vCapturo As Boolean = PrintWindow(pHandle, dc, Nothing)
  19.  
  20.        memoryGraphics.ReleaseHdc(dc)
  21.        memoryGraphics.Dispose()
  22.  
  23.        Return bmp
  24.    End Function
  25. End Class

vas a tener que pasarlo a C#

&H101A2 es el handle de la ventana que queres capturar, en este caso probe con firefox.

saludos.


En línea

arenoide

Desconectado Desconectado

Mensajes: 167



Ver Perfil
Re: API PrintWindow
« Respuesta #2 en: 18 Julio 2011, 01:00 am »

Funciona de categoría! La ventana ni se inmuta! Mil gracias seba!!!
Por si a alguien en un futuro le interesa, el código pasado a C# y retocado un poco para que funcione en función estática quedaría así:

Código
  1.       public static Bitmap CapturarPantalla(IntPtr pHandle)
  2.        {
  3.            Bitmap bmp = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));
  4.            Graphics memoryGraphics = Graphics.FromImage(bmp);
  5.            IntPtr dc = memoryGraphics.GetHdc();
  6.  
  7.            bool vCapturo = PrintWindow(pHandle, dc, 0);
  8.  
  9.            memoryGraphics.ReleaseHdc(dc);
  10.            memoryGraphics.Dispose();
  11.  
  12.            return bmp;
  13.        }
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines