|
-
Jul 13th, 2011, 11:03 AM
#1
Thread Starter
New Member
Screenshot taking
Hi i found some code to take screenshot from screen it works perfectly - but needs some changes and my vb.net - skill is pretty low -can someone help me plz? Code is here:
Code:
Imports System.Runtime.InteropServices
Module Module1
<DllImport("user32.dll")> _
Public Function GetDesktopWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function GetDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function GetWindowDC(ByVal ptr As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
End Function
<DllImport("gdi32.dll")> _
Public Function BitBlt(ByVal hdcDest As IntPtr, ByVal xDest As Integer, ByVal yDest As Integer, ByVal wDest As Integer, ByVal hDest As Integer, ByVal hdcSource As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal rop As CopyPixelOperation) As Boolean
End Function
Public Function GetScreenSnapshot(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer)
Dim hwNd As Long
Dim targetDC As Long
Dim picHandle As Long
Dim picHDC As Long
Const vbSRCCOPY As Long = &HCC0020
hwNd = GetDC(GetDesktopWindow)
targetDC = GetWindowDC(hwNd)
picHandle = Form2.PictureBox1.Handle
picHDC = GetDC(picHandle)
BitBlt(picHDC, 0, 0, width, height, hwNd, x, y, vbSRCCOPY)
ReleaseDC(picHandle, picHDC)
ReleaseDC(hwNd, targetDC)
End Function
End Module
so now as it is - it taking part of screen and then drawing it into Form2.PictureBox1 - i need this code to return bitmap( without using any picture boxes) just bitmap of screened area. Any help? ( i put bold that part of code that probably need be changed to return bitmap instead of drawing image to picture box)
P.S I probably can do like this "Mbitmap = picturebox1.image" аnd then "return Mbitmap" - but it not good solution -need smth that writes directly to bitmap without using picture box.
P.S Sry for my bad english
Last edited by biggreen; Jul 13th, 2011 at 11:07 AM.
-
Jul 13th, 2011, 01:19 PM
#2
Re: Screenshot taking
 Originally Posted by biggreen
i need this code to return bitmap( without using any picture boxes) just bitmap of screened area. Any help?
See if one these works any better...
Code:
Public Class Form1
Private Function GetScrnShot(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim img As Bitmap = New Bitmap(width, height)
Dim gr As Graphics = Graphics.FromImage(img)
gr.CopyFromScreen(New Point(x, y), New Point(0, 0), New Size(width, height))
Return img
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' test...
PictureBox1.Image = GetScrnShot(0, 0, 100, 100) ' get top-left 100x100 pixels.
End Sub
End Class
Or ....
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Const vbSRCCOPY As Integer = &HCC0020
<DllImport("gdi32.dll")> _
Private Shared Function BitBlt(ByVal hdc As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Integer) As Boolean
End Function
Private Function GetScreenSnapshot(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim bmp As New Bitmap(width, height)
Dim gdest As Graphics = Graphics.FromImage(bmp)
Dim gsrc As Graphics = Graphics.FromHwnd(IntPtr.Zero)
Dim hSrcDC As IntPtr = gsrc.GetHdc()
Dim hDestDC As IntPtr = gdest.GetHdc()
BitBlt(hDestDC, 0, 0, width, height, hSrcDC, x, y, vbSRCCOPY)
gdest.ReleaseHdc()
gdest.Dispose()
gsrc.ReleaseHdc()
gsrc.Dispose()
Return bmp
End Function
' test GetScreenSnapshot...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mybitmap As Bitmap = GetScreenSnapshot(0, 0, 100, 100) ' get top-left 100x100 pixels.
PictureBox1.Image = mybitmap
End Sub
End Class
Last edited by Edgemeal; Jul 13th, 2011 at 01:31 PM.
-
Jul 13th, 2011, 03:51 PM
#3
Thread Starter
New Member
Re: Screenshot taking
i try second -thx -1st will not take screens from dx apps -tryed smth like it ))
add: BIggest thanks ever - 2nd variant works perfectly ))
Last edited by biggreen; Jul 13th, 2011 at 04:01 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|