Results 1 to 14 of 14

Thread: Capture The Screen ! My Code is fast but not working !

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Geneve , Switzerland
    Posts
    64

    Capture The Screen ! My Code is fast but not working !

    I combained some codes from each of you to build a really fast screen monitor , but so far It did not work !

    Module
    Code:
    Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
    Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    Public Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
    Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
    
    
    Public Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Public Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    Form : MonitorScreen
    Code:
    Dim ProgramDC As Long
    Dim ScreenDC As Long
    Dim ImageDC As Long
    Dim CapErr As Long
    
    Option Explicit
    
    Private Type SAFEARRAYBOUND
        cElements As Long
        lLbound As Long
    End Type
    
    Private Type SAFEARRAY2D
        cDims As Integer
        fFeatures As Integer
        cbElements As Long
        cLocks As Long
        pvData As Long
        Bounds(0 To 1) As SAFEARRAYBOUND
    End Type
    
    Private Type BITMAP
        bmType As Long
        bmWidth As Long
        bmHeight As Long
        bmWidthBytes As Long
        bmPlanes As Integer
        bmBitsPixel As Integer
        bmBits As Long
    End Type
    
    Public pic() As Byte
    Dim sa As SAFEARRAY2D
    Dim bmp As BITMAP
    Dim r As Long, g As Long, b As Long
    Dim i As Integer
    Dim j As Integer
    Dim errmsg As Integer
    
    
    
    Private Sub Cap_Click()
    Refresher.Interval = 42
    End Sub
    Private Sub Form_Load()
    ProgramDC = MonitorScreen.hDC
    ScreenDC = GetWindowDC(0)
    ImageDC = ImgField.hDC
    End Sub
    Private Sub Pause_Click()
    Refresher.Interval = 0
    End Sub
    Private Sub PrtComm_Click()
    Prt.Show
    Prt.PrtForm.Text = ""
    For i = 1 To 240
    For j = 1 To 320
    Prt.PrtForm.Text = pic(i, j)
    Next j
    Next i
    
    End Sub
    
    Private Sub Refresher_Timer()
    CapErr = BitBlt(ImageDC, 0, 0, 320, 240, ScreenDC, 33, 214, vbSrcCopy)
    
    GetObjectAPI ImgField.Picture, Len(bmp), bmp
    With sa
        .cbElements = 1
        .cDims = 2
        .Bounds(0).lLbound = 0
        .Bounds(0).cElements = bmp.bmHeight
        .Bounds(1).lLbound = 0
        .Bounds(1).cElements = bmp.bmWidthBytes
        .pvData = bmp.bmBits
    End With
    
    CopyMemory ByVal VarPtrArray(pic), VarPtr(sa), 4
    
    End Sub
    the result is sad , PIC array contains nothing !

    Thanks a lot for reading my rewbie code at first place !
    What are the universe used for and why it's here ?

  2. #2
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    i was going to post this for you b4 but hesitated becuase you wanted just the image box but hey:
    This is just as fast perhaps better plus has less code to capture the screen.

    Code:
    Private Declare Sub keybd_event Lib "user32" _
    (ByVal bVk As Byte, ByVal bScan As Byte, _
    ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Const VK_MENU = &H12
    Private Const VK_SNAPSHOT = &H2C
    Private Const KEYEVENTF_KEYUP = &H2

    Public Function SaveScreen(sFile As String) As Boolean
    Dim sPic As IPictureDisp
    On Error GoTo Errhandler
    Clipboard.Clear
    keybd_event VK_MENU, 0, 0, 0
    DoEvents
    keybd_event VK_SNAPSHOT, 1, 0, 0
    DoEvents
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
    DoEvents
    Set sPic = Clipboard.GetData(0)
    SavePicture sPic, sFile
    Clipboard.Clear
    Set sPic = Nothing
    SaveDesktop = True
    Errhandler:
    End Function

    Usage:

    Private Sub Command5_Click()
    Form1.WindowState = 2
    SaveScreen "c:\my_pic.bmp"
    End Sub

    Hows that?

  3. #3
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879
    I didn't even read your code but try executing the Print Screen button from code. That's really fast 2! lol and alot less coding!
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  4. #4
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    nope from memory if you use sendkeys to the printscrn button or ctrl + pntscrn it gives an error or doesnt work 1 or the other.

    Thats why you gots to do like i have!

    And if you want to put the screen capture straight into a image box you set the image boxes path to clipboard.

    i.e image1.path = clipboard.getdata or something similar too that!

  5. #5
    Tygur
    Guest
    Executing the Print Screen button is not the way to go. It uses the clipboard, and that can get annoying for the person using the program.

    VB is a perfect example of this. I don't think anybody appreciates that VB clears the clipboard as it loads up. Do you really want your program to be just as annoying?

  6. #6
    Tygur
    Guest
    Cooker, I've been looking at your code and I'm having troule figuring out what you're trying to do in the timer. What are you trying to get into the Pic array? What do you intend on doing to it after it gets filled with whatever has to go into it? Maybe there's another way of doing whatever youre trying to do.

  7. #7
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    and whats wrong with my way tygur?
    Simple easy!
    Better than using bitblt!
    I dont say clipboard stuff his uses the memory instead if i'm correct!

  8. #8
    Tygur
    Guest
    Your way clears the clipboard, right?
    If so, that's what's wrong. I would never want to use a program that has to clear the clipboard just to capture the screen.

  9. #9
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Doesnt have too i just put that there to be sure get rid of the lines of code clipboard.clear then use it if it's that worrying.
    Still works.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Geneve , Switzerland
    Posts
    64

    Unhappy Well ...

    I wanted to save the colors into an array ...
    So what is the point of saving it onto the harddisk at first place .
    What are the universe used for and why it's here ?

  11. #11
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    As i said above You Dont Have To Save It!

    You can always use clipboard.getdata and use that to put it into your array or whatever. It's how i save my screen captures thats all!

    image1.picture = clipboard.getdata or something similar!

  12. #12

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Geneve , Switzerland
    Posts
    64

    Unhappy ...

    Still , I don't think pressing the printscreen 24 times a second is a good Idea ...
    What are the universe used for and why it's here ?

  13. #13
    Tygur
    Guest
    Beacon, your code takes away whatever was in the clipboard before you tried to capture the screen. Cooker's code is in a timer. If you put your code in a timer, you effectively render the clipboard useless.

    Cooker, what you mean by "save the colors into an array"? Are you trying to store the image in a variable so you can show it someplace else (or save it)? If so, there are easier ways.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Geneve , Switzerland
    Posts
    64

    Unhappy My aim

    My aim is real time screen analyzer that capture a part of the screen , analyze it , and output the result. I need the system to refresh really fast so the system will quickly respond to anything on the screen.
    A array is need for the analysis
    What are the universe used for and why it's here ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width