Results 1 to 33 of 33

Thread: Save a screenshot?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Save a screenshot?

    im trying to make a hotkey to save a screenshot of a fullscreen directx game.
    this game is not mine and therfore dont have access to its source code.

    but i do know api and i can get a processhandle from this application. i have also hooked the keyboard in this game so the hotkey is not a problem.

    the only thing i need to know how to do is save a screenshot? anyone know? thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    This worked from code...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
    4. ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    5.  
    6. Private Const VK_SNAPSHOT = &H2C
    7.  
    8. Private Function SaveScreen(ByVal pstrFileName As String) As Boolean
    9. 'To get the Active Window
    10. Call keybd_event(vbKeySnapshot, 0, 0, 0)
    11.  
    12. SavePicture Clipboard.GetData(vbCFBitmap), pstrFileName
    13.  
    14. SaveScreen = True
    15. Exit Function
    16. End Function
    17.  
    18. Private Sub Command1_Click()
    19. Call SaveScreen("C:\code\vb6\shot1.bmp")
    20. End Sub

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save a screenshot?

    Since your program doesn't have the focus when you want to save the screen. You might want to look into using the RegisterHotKey function to register a hotkey to be used to save the screen. You could then use the code posted by Hack above in the callback function to save the image.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Save a screenshot?

    What makes you so sure I dont have focus? Like i said, i have already hooked the keyboard for use of it ingame, so i can just press a button and it saves a screen.

    so yes i do have focus. i will try the code now, thanks

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save a screenshot?

    I'm sorry I didn't read your origional post careful enough.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Save a screenshot?

    Does anyone know if i could possibly blueprint all these images im saving with some simple text at the bottom or something?

    I have the filenames saved based on the time right now.

    BTW can i save them as jpg instead of bmp because bmp is large files

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save a screenshot?

    You could put the image inside a memory dc and use DrawText and then store it or simply put it in a hidden PictureBox and use the Print statement and then save the image (the PicBox need to have the AutoRedraw property set). To save as a JPG you need some third party control. I think I have a simple BMP2JPG dll somewhere, I'll see if I can dig it out for you.

  8. #8
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Save a screenshot?

    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Save a screenshot?

    Here's the DirectX approach to saving snapshots. You can save it as bmp as well as jpg or any other format. DirectX does it for you.

    VB Code:
    1. Public Sub DirectX_Snapshot(ByVal File_Path As String)
    2.  
    3.     Dim Surface As Direct3DSurface8
    4.     Dim SrcPalette As PALETTEENTRY
    5.     Dim SrcRect As RECT
    6.     Dim Direct3D_Display_Mode As D3DDISPLAYMODE
    7.  
    8.     'get display dimensions
    9.     Direct3D_Device.GetDisplayMode Direct3D_Display_Mode
    10.  
    11.     'create a surface to put front buffer on,
    12.     'GetFrontBuffer always returns D3DFMT_A8R8G8B8
    13.     Set Surface = Direct3D_Device.CreateImageSurface(Direct3D_Display_Mode.Width, Direct3D_Display_Mode.Height, D3DFMT_A8R8G8B8)
    14.  
    15.     'get data from front buffer
    16.     Direct3D_Device.GetFrontBuffer Surface
    17.  
    18.     'we are saving entire area of this surface
    19.    
    20.     With SrcRect
    21.    
    22.         .Left = 0
    23.         .Right = Direct3D_Display_Mode.Width
    24.         .Top = 0
    25.         .bottom = Direct3D_Display_Mode.Height
    26.        
    27.     End With
    28.  
    29.     'save this surface to a BMP file
    30.     'Direct3DX.SaveSurfaceToFile File_Path, D3DXIFF_BMP, Surface, SrcPalette, SrcRect
    31.  
    32.     'save this surface to a JPG file
    33.     Direct3DX.SaveSurfaceToFile File_Path, D3DXIFF_JPG, Surface, SrcPalette, SrcRect
    34.  
    35. 'Here are some more formats
    36.  
    37. 'D3DXIFF_TGA
    38. 'D3DXIFF_PNG
    39. 'D3DXIFF_DIB
    40. 'D3DXIFF_DDS
    41. 'D3DXIFF_PPM
    42. 'D3DXIFF_FORCE_DWORD <---- Don't know what that does.
    43.  
    44. End Sub

    And in your game loop, you put this in:

    VB Code:
    1. 'Declare this variable in the General Declarations
    2.  
    3.        Dim Snapshot_Number As Long
    4.  
    5.        'Then put this in your loop
    6.  
    7.         If DirectInput_Key_State(DIK_F12) Then
    8.            
    9.             If Dir$(App.Path & "\Snapshots\", vbDirectory) = "" Then
    10.                
    11.                 MkDir App.Path & "\Snapshots\"
    12.                
    13.             End If
    14.            
    15.             If Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".jpg") = "" Then
    16.            
    17.                 DirectX_Snapshot App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".jpg"
    18.            
    19.             End If
    20.            
    21.             While Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".jpg") <> ""
    22.                
    23.                 DoEvents
    24.                
    25.                 Snapshot_Number = Snapshot_Number + 1
    26.                
    27.             Wend
    28.            
    29.         End If

  10. #10
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    Hi Jacob Roman,

    If any of us want to build a stand alone pgm using your code, is there any other things we need to declare (or know), to build into the pgm.

    Your sample aimed at jacob123, assumes that he has Directx 'stuff' already in his pgm (which he has).

    Thanks for sharing,
    Rob C

  11. #11
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Save a screenshot?

    Well if you wanted to use my code, you must have some knowledge of DirectX. Otherwise I'm not sure how it'll work with a stand alone app to be honest with ya.

    And I just found out that I need to know the correct color format to get that code working. Already posted this problem in www.gamedev.net. It'll be solved shortly, I hope.

    The format D3DFMT_A8R8G8B8 works very well with bmp files. Jpg it's something else.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Save a screenshot?

    Does this 'DirectX approach' have any advantages? The code that Hack posted has been working fine.

    Is it possible to blueprint my screenshots with an actual IMAGE? Maybe even an img with some low-level png transparency?

  13. #13
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Save a screenshot?

    I was just pointing out another method, which DirectX has. They both should do the same thing. The only advantage is the fact that it supports multiple formats that gets converted for you by DirectX.

    My question in gamedev got no replys

  14. #14

  15. #15
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    Nice one Martin.

    Two things -

    If user captures the wrong Active Window, or just wants to re-capture that window (content changed), or capture another active Window, the 'focus' is still on that option. That prevents a re-click.
    I have added an option 4 to the control array. Placed it at the bottom/center of Frame1. Set it's style to Graphical, and made it's Caption 'Clear'
    The large button 'Clear Picture Box' can be removed, as this smaller Clear button does the same thing, and also frees up the options. Thus any of them can be re-clicked after the new Clear is used.

    Can anyone add the ability to Save the image as jpg ?
    (That would make me a very happy 'mother' this Mother's Day)
    Last edited by RobCrombie; Apr 27th, 2005 at 10:09 PM.
    Rob C

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Save a screenshot?

    Post the new project, and I'll see about adding the dll. I think I have a project using it somewhere else.

  17. #17
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    Ta,
    See attached
    Attached Files Attached Files
    Rob C

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Save a screenshot?

    Not bad. Make sure you put the dll in the system32 folder.

  19. #19
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    Martin, dglienna,

    This pgm is now just to good.

    If you both agree, I think we should delete this entire thread, before too many people get a chance to download it ?
    Rob C

  20. #20
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Save a screenshot?

    Why? Someone else may want to add something else in the future.
    I had an old version, before your update. Now it's better than before and it also does jpeg. That's what the forum is all about.

  21. #21
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    I have been accused of having a dry sense of humour <======
    Rob C

  22. #22
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    I have added many enhancements to the Saving area.
    You can still use the Dialog.
    However it remembers where you put it (in txtFolder), and what you called it (in txtFile).
    You can then quickly Save subsequent captures, into the same folder.
    I have also added a button to allow you to just save into the app.path

    When the Form closes it Saves the Folder and File names used, and restores them (in those textboxes), when the pgm next runs.

    I believe that a capture pgm has to be quick and easy to use, when you really need it. At those times, you don't want to be stuffing around browsing and naming all your images.
    Thus there is also a 'lazy' feature that allows you to just tell it to save the flippin image, and not bother you with minor details, like having to tell it where to put it, or what to call it.
    If no folder has been specified, it uses app.path
    If no file has been specifed, it uses yyyymmdd.jpg

    AH! you might say 'What happens if I am lazy twice ?'
    That is, when you use the same file name (or no file name, and it uses the yyyymmdd again)
    There is an auto increment feature that adds an incrementing number, whenever it detects a clash.
    Thus if you were super lazy (like me), it would create in app.path -
    20050428.jpg
    20050428_001.jpg
    20050428_002.jpg

    If you have specified a File name in txtFile, then it does the same, but uses that File name.

    It could have more commenting, than it has. So I figure you'll make some small deductions from your PayPal transfers (to punish me).

    I am attaching the DLL separately, as some of you may have downloaded that already.

    There may be some bugs (it is 4:00 AM in OZ). (More PayPal deductions ?)
    Attached Images Attached Images  
    Attached Files Attached Files
    Rob C

  23. #23
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    'But wait there's more'

    I have added 'Minimize to SysTray'

    Icon is always in the SysTray (when pgm is running).
    It never appears in the TaskBar.

    Click the Icon to show, and click to hide.
    Attached Files Attached Files
    Rob C

  24. #24
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    Pretty soon, it'll be ready for the CodeBank!
    just make sure to give credit where credit is due
    'Oh yee of little faith'
    In this link
    Minimize To SysTray
    I said
    in 'my' (cough!) project
    So that should arrest your fears (humour again)
    Last edited by RobCrombie; Apr 29th, 2005 at 06:31 AM.
    Rob C

  25. #25
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    The click to hide and click to show, was ok, except you had to hide some Apps to see our Form.
    I have enhanced the attached, to hopefully fix that inconvenience

    Let me know if it is still hard to find(see), when un-hiding 'our' Form.
    When you are singing about 'us' around the camp fire, the 'our'' refers to Martin, David and Rob.

    PS Don't forget to download David's DLL, which I attached separately above Bmp2Jpeg.zip
    Attached Files Attached Files
    Last edited by RobCrombie; Apr 30th, 2005 at 12:14 AM.
    Rob C

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Save a screenshot?

    You really should include the jpeg.dll in the zip file, as most people will not have it, which will cause an error. It looks good.

  27. #27
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    Hi David,
    I have edited the above post, to include your two suggestionsi
    Rob C

  28. #28
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Save a screenshot?

    Perhaps the finished product for the screen capturing should be popped into the CodeBank where it could be more easily found.

  29. #29
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Save a screenshot?

    Quote Originally Posted by Hack
    Perhaps the finished product for the screen capturing should be popped into the CodeBank where it could be more easily found.
    I think the UtilityBank would be better.

  30. #30
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Save a screenshot?

    Hi,
    I have a couple of improvements I'm working on.
    Also I think the middle two options are not much use, or am I missing something ?

    An nice additional option, would be the ability to use mouse to define an area of the screen to capture.
    Anyone got a sample that I could slot into this project ?
    Rob C

  31. #31
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Save a screenshot?

    Quote Originally Posted by RobCrombie
    An nice additional option, would be the ability to use mouse to define an area of the screen to capture.
    Anyone got a sample that I could slot into this project ?
    I did something like that a few years back. What I basically did was to take a snapshot of the entire screen and put it on a non-border topmost fullscreen Form which the user could select an area from.

  32. #32
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Save a screenshot?

    Hi Hack,

    I am facing problem with the below line. I get vb error 380: invalid property value.
    SavePicture Clipboard.GetData(vbCFBitmap), pstrFileName


    I like to take the screenshot of only my working form. Is it possible ? Please help.

  33. #33
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Save a screenshot?

    Thats because you need to put a DoEvents in there.
    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    4. ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    5.  
    6. Private Sub SaveScreen(ByVal pstrFileName As String)
    7.  
    8. 'To get the Active Form
    9. Call keybd_event(vbKeySnapshot, 0, 0, 0)
    10.  
    11. 'To get the Full Screen
    12. 'Call keybd_event(vbKeySnapshot, 1, 0, 0)
    13.  
    14. Clipboard.Clear
    15. DoEvents
    16. SavePicture Clipboard.GetData(vbCFBitmap), pstrFileName
    17.  
    18. End Sub
    19.  
    20. Private Sub Command1_Click()
    21.  
    22. Call SaveScreen("C:\shot1.bmp")
    23.  
    24. End Sub
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

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