|
begin process at 2008 07 19 08:51:33
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
SCREENSHOT / SNAPSHOT SANS CLIPBOARD NI SENDKEYS
Information sur la source
Description
Voici un petit code bien pratique permettant de faire des captures d'écran, sans utiliser le presse papier. Il permet de capturer l'ecran entier (ouf..!),[EDIT] la fenetre active, ou une partie de l'ecran seulement, en determinant le point de location (x,y) et la dimension (width, height). J'ai joint un sample montrant comment l'utilisé et comment convertir l'image vers le format voulu. Merci edit : Attention, la source contient DEUX classes differentes, une à utiliser en VB 2002-2003, l'autre en VB 2005 Le sample utilise la version vb2002-2003, et sera mis à jour en fin d'année scolaire avec un de mes projets de fin d'année ^^
Source
- 'Device Context = "contexte de dispositif" selon le traducteur de google.fr
- 'Pour plus d'informations :
- 'http://msdn.microsoft.com/library/en-us/gdi/devcons_0g6r.asp?frame=true
-
- 'CLASSE VB.NET 2003 - 1.1 (et 2002? 1.0)
- Public Class ScreenShoter
- Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, ByVal SrcX As Integer, ByVal SrcY As Integer, ByVal Rop As Integer) As Integer
- Private Declare Function GetForegroundWindow Lib "user32" () As IntPtr
- Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As Rectangle) As Integer
- Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
- 'Capture tout l'écran
-
-
- Public Shared Function ShotScreen() As Bitmap
- Try
- Dim DesktopRect As Rectangle = Screen.GetBounds(New Point(0, 0)) 'obtient la taille du bureau sous forme de rectangle dans DesktopRect
- Return ShotScreenPart(DesktopRect.Width, DesktopRect.Height) 'appele la fonction ShotScreenPart avec les dimensions du bureau.
- Catch ex As Exception
- MsgBox(ex.ToString)
- Return emptybitmap()
- End Try
- End Function
-
- 'Capture la fenetre active
- Public Shared Function ShotActiveWin() As Bitmap
- Dim WinRect As Rectangle
- Try
- If (GetWindowRect(GetForegroundWindow, WinRect) > 0) Then 'obtient la taille et la position de la fenetre active sous forme de rectangle (WinRect)
- Return ShotScreenPart(WinRect.Size.Width - WinRect.Left, WinRect.Size.Height - WinRect.Top, WinRect.Left, WinRect.Top) 'appele la fonction ShotLoc avec les dimensions et la position de la fenetre.
- Else
- Return ShotScreen()
- End If
- Catch ex As Exception
- MsgBox(ex.ToString)
- Return emptybitmap()
- End Try
- End Function
-
- Public Shared Function ShotScreenPart(ByVal nwidth As Integer, ByVal nheight As Integer, Optional ByVal x As Integer = 0, Optional ByVal y As Integer = 0) As Bitmap
- Dim resultBmp As Bitmap = New Bitmap(nwidth, nheight) 'crée l'objet bitmap cible
- Dim SrcGraph As Graphics = Graphics.FromHwnd(GetDesktopWindow) 'crée l'objet "graphics" SelGraph a partir du handdle du bureau
- Dim BmpGraph As Graphics = Graphics.FromImage(resultBmp) 'crée un objet graphics à partir du bitmap
- Dim bmpDC As IntPtr = BmpGraph.GetHdc() 'obtient le device context du bitmap
- Dim hDC As IntPtr = SrcGraph.GetHdc() 'obtient le device context du bureau
- BitBlt(bmpDC, 0, 0, nwidth, nheight, hDC, x, y, &HCC0020) '"bit-block transfer" : copie chaque bits affichés dans le device context hDC dans le device context du bitmap
- SrcGraph.ReleaseHdc(hDC) 'relache le device context du bureau
- BmpGraph.ReleaseHdc(bmpDC) 'relache le device context du bitmap
- SrcGraph.Dispose()
- BmpGraph.Dispose() 'libere toutes les ressources crées par l'objet (useless?)
- Return resultBmp
- End Function
-
- Public Shared Function emptybitmap() As Bitmap
- Dim resultBmp As Bitmap = New Bitmap(1, 1) 'crée l'objet bitmap cible
- Return resultBmp
- End Function
-
- Public Shared Function TagShoot(ByVal curshot As Bitmap, ByVal tag As String, ByVal x As Single, ByVal y As Single, ByVal txtcolor As Color) As Bitmap
- Dim graph As Graphics = Graphics.FromImage(curshot)
- Dim drawFont As New Font("Arial", 16)
- Dim drawBrush As New SolidBrush(txtcolor)
- graph.DrawString(tag, drawFont, drawBrush, x, y)
- graph.Flush()
- Return curshot
- End Function
- End Class
-
-
-
-
- CLASSE VB.NET 2005 - 2.0
- Public Class ScreenShoter2
- Private Declare Function GetForegroundWindow Lib "user32" () As IntPtr
- Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As Rectangle) As Integer
-
- 'Capture tout l'écran
- Public Shared Function ShotScreen() As Bitmap
- Try
- Dim DesktopRect As Rectangle = Screen.GetBounds(New Point(0, 0)) 'obtient la taille du bureau sous forme de rectangle dans DesktopRect
- Return ShotScreenPart(DesktopRect.Width, DesktopRect.Height) 'appele la fonction ShotScreenPart avec les dimensions du bureau.
- Catch ex As Exception
- Return emptybitmap()
- MsgBox(ex.ToString)
- End Try
- End Function
-
- 'Capture la fenetre active
- Public Shared Function ShotActiveWin() As Bitmap
- Dim WinRect As Rectangle
- Try
- If (GetWindowRect(GetForegroundWindow, WinRect) > 0) Then 'obtient la taille et la position de la fenetre active sous forme de rectangle (WinRect)
- Return ShotScreenPart(WinRect.Size.Width - WinRect.Left, WinRect.Size.Height - WinRect.Top, WinRect.Left, WinRect.Top) 'appele la fonction ShotLoc avec les dimensions et la position de la fenetre.
- Else
- Return ShotScreen()
- End If
- Catch ex As Exception
- MsgBox(ex.ToString)
- Return emptybitmap()
- End Try
- End Function
-
- Public Shared Function ShotScreenPart(ByVal nwidth As Integer, ByVal nheight As Integer, Optional ByVal x As Integer = 0, Optional ByVal y As Integer = 0) As Bitmap
- Dim resultBmp As Bitmap = New Bitmap(nwidth, nheight)
- Dim BmpGraph As Graphics = Graphics.FromImage(resultBmp)
- Dim screensize As Size = New Size(nwidth, nheight)
- BmpGraph.CopyFromScreen(x, y, 0, 0, screensize)
- BmpGraph.Dispose()
- Return resultBmp
- End Function
-
- Public Shared Function emptybitmap() As Bitmap
- Dim resultBmp As Bitmap = New Bitmap(1, 1)
- Return resultBmp
- End Function
-
- Public Shared Function TagShoot(ByVal curshot As Bitmap, ByVal tag As String, ByVal x As Single, ByVal y As Single, ByVal txtcolor As Color) As Bitmap
- Dim graph As Graphics = Graphics.FromImage(curshot)
- Dim drawFont As New Font("Arial", 16)
- Dim drawBrush As New SolidBrush(txtcolor)
- graph.DrawString(tag, drawFont, drawBrush, x, y)
- graph.Flush()
- Return curshot
- End Function
-
- End Class
'Device Context = "contexte de dispositif" selon le traducteur de google.fr
'Pour plus d'informations :
'http://msdn.microsoft.com/library/en-us/gdi/devcons_0g6r.asp?frame=true
'CLASSE VB.NET 2003 - 1.1 (et 2002? 1.0)
Public Class ScreenShoter
Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, ByVal SrcX As Integer, ByVal SrcY As Integer, ByVal Rop As Integer) As Integer
Private Declare Function GetForegroundWindow Lib "user32" () As IntPtr
Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As Rectangle) As Integer
Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
'Capture tout l'écran
Public Shared Function ShotScreen() As Bitmap
Try
Dim DesktopRect As Rectangle = Screen.GetBounds(New Point(0, 0)) 'obtient la taille du bureau sous forme de rectangle dans DesktopRect
Return ShotScreenPart(DesktopRect.Width, DesktopRect.Height) 'appele la fonction ShotScreenPart avec les dimensions du bureau.
Catch ex As Exception
MsgBox(ex.ToString)
Return emptybitmap()
End Try
End Function
'Capture la fenetre active
Public Shared Function ShotActiveWin() As Bitmap
Dim WinRect As Rectangle
Try
If (GetWindowRect(GetForegroundWindow, WinRect) > 0) Then 'obtient la taille et la position de la fenetre active sous forme de rectangle (WinRect)
Return ShotScreenPart(WinRect.Size.Width - WinRect.Left, WinRect.Size.Height - WinRect.Top, WinRect.Left, WinRect.Top) 'appele la fonction ShotLoc avec les dimensions et la position de la fenetre.
Else
Return ShotScreen()
End If
Catch ex As Exception
MsgBox(ex.ToString)
Return emptybitmap()
End Try
End Function
Public Shared Function ShotScreenPart(ByVal nwidth As Integer, ByVal nheight As Integer, Optional ByVal x As Integer = 0, Optional ByVal y As Integer = 0) As Bitmap
Dim resultBmp As Bitmap = New Bitmap(nwidth, nheight) 'crée l'objet bitmap cible
Dim SrcGraph As Graphics = Graphics.FromHwnd(GetDesktopWindow) 'crée l'objet "graphics" SelGraph a partir du handdle du bureau
Dim BmpGraph As Graphics = Graphics.FromImage(resultBmp) 'crée un objet graphics à partir du bitmap
Dim bmpDC As IntPtr = BmpGraph.GetHdc() 'obtient le device context du bitmap
Dim hDC As IntPtr = SrcGraph.GetHdc() 'obtient le device context du bureau
BitBlt(bmpDC, 0, 0, nwidth, nheight, hDC, x, y, &HCC0020) '"bit-block transfer" : copie chaque bits affichés dans le device context hDC dans le device context du bitmap
SrcGraph.ReleaseHdc(hDC) 'relache le device context du bureau
BmpGraph.ReleaseHdc(bmpDC) 'relache le device context du bitmap
SrcGraph.Dispose()
BmpGraph.Dispose() 'libere toutes les ressources crées par l'objet (useless?)
Return resultBmp
End Function
Public Shared Function emptybitmap() As Bitmap
Dim resultBmp As Bitmap = New Bitmap(1, 1) 'crée l'objet bitmap cible
Return resultBmp
End Function
Public Shared Function TagShoot(ByVal curshot As Bitmap, ByVal tag As String, ByVal x As Single, ByVal y As Single, ByVal txtcolor As Color) As Bitmap
Dim graph As Graphics = Graphics.FromImage(curshot)
Dim drawFont As New Font("Arial", 16)
Dim drawBrush As New SolidBrush(txtcolor)
graph.DrawString(tag, drawFont, drawBrush, x, y)
graph.Flush()
Return curshot
End Function
End Class
CLASSE VB.NET 2005 - 2.0
Public Class ScreenShoter2
Private Declare Function GetForegroundWindow Lib "user32" () As IntPtr
Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As Rectangle) As Integer
'Capture tout l'écran
Public Shared Function ShotScreen() As Bitmap
Try
Dim DesktopRect As Rectangle = Screen.GetBounds(New Point(0, 0)) 'obtient la taille du bureau sous forme de rectangle dans DesktopRect
Return ShotScreenPart(DesktopRect.Width, DesktopRect.Height) 'appele la fonction ShotScreenPart avec les dimensions du bureau.
Catch ex As Exception
Return emptybitmap()
MsgBox(ex.ToString)
End Try
End Function
'Capture la fenetre active
Public Shared Function ShotActiveWin() As Bitmap
Dim WinRect As Rectangle
Try
If (GetWindowRect(GetForegroundWindow, WinRect) > 0) Then 'obtient la taille et la position de la fenetre active sous forme de rectangle (WinRect)
Return ShotScreenPart(WinRect.Size.Width - WinRect.Left, WinRect.Size.Height - WinRect.Top, WinRect.Left, WinRect.Top) 'appele la fonction ShotLoc avec les dimensions et la position de la fenetre.
Else
Return ShotScreen()
End If
Catch ex As Exception
MsgBox(ex.ToString)
Return emptybitmap()
End Try
End Function
Public Shared Function ShotScreenPart(ByVal nwidth As Integer, ByVal nheight As Integer, Optional ByVal x As Integer = 0, Optional ByVal y As Integer = 0) As Bitmap
Dim resultBmp As Bitmap = New Bitmap(nwidth, nheight)
Dim BmpGraph As Graphics = Graphics.FromImage(resultBmp)
Dim screensize As Size = New Size(nwidth, nheight)
BmpGraph.CopyFromScreen(x, y, 0, 0, screensize)
BmpGraph.Dispose()
Return resultBmp
End Function
Public Shared Function emptybitmap() As Bitmap
Dim resultBmp As Bitmap = New Bitmap(1, 1)
Return resultBmp
End Function
Public Shared Function TagShoot(ByVal curshot As Bitmap, ByVal tag As String, ByVal x As Single, ByVal y As Single, ByVal txtcolor As Color) As Bitmap
Dim graph As Graphics = Graphics.FromImage(curshot)
Dim drawFont As New Font("Arial", 16)
Dim drawBrush As New SolidBrush(txtcolor)
graph.DrawString(tag, drawFont, drawBrush, x, y)
graph.Flush()
Return curshot
End Function
End Class
Conclusion
merci à jesusonline pour l'utilisation des objet graphics !
Historique
- 24 mars 2005 03:12:32 :
- code commenté + ajout de capture de la fenetre active
- 24 mars 2005 04:01:57 :
- .
- 29 mars 2005 02:11:28 :
- _optimisation du code
_j'ai refait la procedure de capture de la fenetre active, je ne fais plus que determiner sa position et sa taille et je la copie du device context du bureau
_code plus "dotnet"...
- 05 février 2007 11:30:11 :
- Ajout de la version .net 2.0 simplifié, et modification de la version 2003 avec une gestion d'erreur simple (valide option strict).
J'ai fait un sample beaucoup plus complet que le premier, mais je n'ose pas le poster pour deux raison :
_L'IHM laisse encore à desirer
_Je suis censé le presenter à la fin de l'année pour les exams, j'ai peur qu'un type montre mon propre projet avant moi lol... (oui oui je suis parano)
- 16 mars 2007 12:40:55 :
- _suppression des avertissements dans le sample et mise à jour de la classe dans celui ci (j'avais laissé une ancienne version), "à la main" car pas de vs2003 installé sur cette machine
Sources de la même categorie
Commentaires
|
|