Click here to Skip to main content
Click here to Skip to main content

A Screen Capture Utility

By , 19 Oct 2007
 

Screenshot - JPG_capture.jpg

Introduction

JPG Capture is a utility designed for developers to capture debugging screenshots, while it is also ideal for general screen capture purposes. JPG Capture can capture the desktop screen into picture files in sequence. Users can define the capture hotkey, capture area (current window, full screen, or rectangular area), picture format (JPG, GIF, BMP etc.), and also the destination.

Setup the hotkey

The program uses a Win32 API function called RegisterHotKey to setup the system wide hotkey.

<DllImport("user32", EntryPoint:="RegisterHotKey", _
SetLastError:=True, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
    ByVal Id As Int32, _
    <MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
    <MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean
End Function

The program passes the window handle of an invisible window called inVisibleHotkey to the RegisterHotKey function. Whenever the hotkey is pressed, a Windows message will be sent to the inVisibleHotkey window, which will trigger the HotKey_Press() routine.

The user can define the hotkey combination using two parameters, fsModifiers and vkey. fsModifiers can be the Ctrl, Alt, or Shift key. vkey can be any other key.

Public Enum HotkeyModifierFlags
    MOD_ALT = &H1
    MOD_CONTROL = &H2
    MOD_SHIFT = &H4
    MOD_WIN = &H8
End Enum

Public Enum HotkeyVkeyFlags
    Key_O = &H4F
    Key_G = &H47
    Key_T = &H54
End Enum

Define the capture area

The capture area can be the current window, full screen, or a rectangular area.

  • The current window option uses a Win32 API function called GetForegroundWindow to define the capture area.
  • The full screen option uses a Win32 API function called GetDesktopWindow to define the capture area.
  • The rectangular area option uses a transparent window called inVisibleCapWin to define the capture area. When the capture starts, the inVisibleCapWin window will be loaded. Users can use a mouse to draw a rectangle to define the capture area. After the mouse button is released, the inVisibleCapWin window will be closed and continues the capture process.

Capture process

The program uses a Win32 API function called BitBlt to capture the desktop screen according to the selected capture area.

<DllImport("gdi32.dll")> Public Shared _
Function BitBlt(ByVal hObject As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, _
        ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hObjectSource As IntPtr, _
        ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Integer) As Boolean
End Function

The screenshot file is saved in sequence. The program uses a routine called makeup_filename to generate the filename in sequence.

Function makeup_Filename(ByRef Scr_num As Integer, ByVal Dir_Path As String, _
            ByVal Prefix_fn As String, ByVal Subfix_fn As String) As String
'combine and generate the filename in sequence

    Dim FileName As String
    Dir_Path = Standardize_path(Dir_Path)
    If Not Directory.Exists(Dir_Path) Then
        Directory.CreateDirectory(Dir_Path)
        'make sure the directory do exist.

        Scr_num = 0
        'reset the picture number since the new dir is empty.
    End If

Next_num:

    If Scr_num < 0 Then Stop
    Scr_num = Scr_num + 1
    FileName = Dir_Path & Prefix_fn
    FileName = FileName & Format(Scr_num, "000")
    FileName = FileName & "." & Subfix_fn
        
    If File.Exists(FileName) Then GoTo Next_num
    'make sure NOT to overwrite the exsisting file

    Return FileName
End Function

Config file

The settings of the program is saved into a config file called config.ini. If the config file is missing, the default config file will be created.

    Dir_Path = C:\Lexer_trace\
    Prefix_fn = lexer
    Subfix_fn = jpg
    hotfsModifiers = 2
    hotVkey = 71
    Area = 2

Future improvements

There are some other functions that can be developed, like a popup preview window before saving the file, resizing the screenshot file as desired, etc.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ken.Huang
Unknown
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGreat ArticlememberShane Story8 Oct '09 - 6:37 
This is a great article in that it shows how to do system wide hotkeys, very well and the application is also very useful. Since it is in DotNet, it can be embellished to one's heart's content. It would be easy for a person to permanently superimpose some image, or save to a file that keeps the annotations so that they are movable (not fixed as part of the image). It would be able to add redact so that you can black out a portion of the saved screen also.. Of course it is true that Gimp or Paint.NET could be used with the captured image to doctor it up as well.
 
Good job!
 
Shane
Jesus loves you! John 3:16 · Rom 3:23 · Rom 6:23 · Rom 10:9

GeneralScreen capture utilitymember42888 Nov '07 - 5:18 
Thanks this is what i was looking for to have some help on my code!
 
Thanks very much Big Grin | :-D
 
---

GeneralA Screen Capture Utility: future improvementsmembererwin DEWOLF23 Oct '07 - 0:42 
Ken,
 
Nice work !
 
Ideas for future improvements:
- (direct) printing instead of an image file
- minimizing in the icon tray instead of the status bar
GeneralRe: A Screen Capture Utility: future improvementsmemberKen.Huang25 Oct '07 - 8:29 
erwin,
 
Thank you for your advice.
GeneralRe: A Screen Capture Utility: future improvementsmemberzigzag26 Feb '08 - 22:46 
Hai,
Really a great work in the field of screen capturing technique. i got more ideas after i saw this project. Thanks for sending such a good project .hope you continue of sending such great projects to help all programmers/developers in this world.....

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 19 Oct 2007
Article Copyright 2007 by Ken.Huang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid