Accueil > > > SCREENSHOT / SNAPSHOT SANS CLIPBOARD NI SENDKEYS
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 du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Screenshot (capture d'écran) avec directx [ par BadNews ]
Bonjours, je cherche un moyen de prendre des captures d'écran en directx. Lorsque j'utilise un logiciel ou jeux en directx/opengl, les fonctions
Faire une capture "d'écran" d'une fenêtre qui est recouverte [ par azerty25 ]
Hello allJe me suis demandé comment faire pour capturer l'image d'une fenetre qui n'est pas visible car recouverte par une autre (mais pas confon
Capture d'écran [ par allfab ]
Bonjour à Tous, je cherche mon bonheur deouis quelques temps mais je ne trouve pas ce qu'il me faut...alors je me suis décidé à &#
Tuto vidéo : Capture vidéo... [ par Blanc ]
Bonjour, je voulais savoir avec qu'elle programme enregistre-t-on l'écran pour faire un tuto vidéo, style une capture d'écran vidé
Capture de tout l'écran et sauvegarde en JPEG sous VB.NET [ par Angel 59 ]
Bonjour à tous,Si quelqu'un peut m'aider en VB.NET, je m'explique :- Je souhaiterais capturer tout l'écran et sauvegarder cette capture en J
Résolution de l'écran [ par Stru ]
Salut,Comment récupérer la résolution de l'écran (Vb ou Api) car avec screen.height et screen.width, il y a un problème sur les valeurs (elles sont ér
est il possible de le faire une capture d'écran comme je le veux!!! [ par nabilac ]
Bonjour,svp comment faire une capture d'écran pour récuperer une partie d'un graphe.mon graphe est constitué de plusieurs picture box reliés entre eux
JAVASCRIPT : ouverture d'une popup selon la taille disponible de l'écran [ par pradayrolus ]
Bonjour, Lorsque l'utilisateur saisit ses login/mot de passe, je souhaiterais ouvrir une fenêtre prenant pour taille la taille disponible de l'écran
Problème pour enregistrer une image de picturebox [ par robapt ]
Slt tout le monde.Voila je vous donne le code directement : If fReceive = True Then cAvance = cAvance + Len(Data) Open "G:\Captures d'écran\Capt
Capture d'écran [ par wawamule ]
Coucou tout le monde :) ,Quelqu'un s'aurait-il comment on peut effectuer une capture d'un écran distant tous les x secondes et envoyer ces images sur
|
Derniers Blogs
XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Je m'y prends un peu tard je sais, mais bon je suis développeur web et donc hyper fainéant ! Toujours dans le cadre des technologies émergentes, ici HTML5, parce qu'on aime HTML5 chez Wyg , nous seront présent, le vieux ( Aurélien V.) et moi, pour pr...
Cliquez pour lire la suite de l'article par Gio [WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|