Save Screenshot to File From Console Application

aychekay

Member
Joined
Oct 10, 2011
Messages
11
Programming Experience
3-5
I'm not too handy with .Net since I do most of my work in MS Access. How can I save a screenshot to a file in a console application (or a code library)? The code I've found uses objects that seem to be specific to the Windows.Forms namespace and I can't seem to import them.

Here's the code I do have. It works on a Windows Form project. I'm using Visual Studio 2008, by the way.

VB.NET:
        If Not System.Windows.Forms.Clipboard.GetDataObject() Is Nothing Then
            Dim oDataObj As IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
            If oDataObj.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap) Then
                Dim oImgObj As System.Drawing.Image = oDataObj.GetData(DataFormats.Bitmap, True)
                Dim sFilename As String = Path.GetTempFileName()
                oImgObj.Save(sFilename, System.Drawing.Imaging.ImageFormat.Gif)
            End If
        End If
 
If you reference System.Drawing assembly you can create a Bitmap, and use Graphics methods FromImage and CopyFromScreen to do a screen capture.
 
I chose to reference System.Windows.Forms, as well as System.Drawing. Here's the end result. This is a "quick and dirty" application, not intended to showcase good design practices.

VB.NET:
Imports System.Windows.Forms
Imports System.IO

Module Module1

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    Private Const VK_SNAPSHOT As Short = &H2CS
    Public Const VK_MENU As Short = &H12  ' <Alt> Key  ->  Hex 12
    Private Const KEYEVENTF_KEYUP As Short = &H2

    Sub Main()
        Try
            keybd_event(VK_MENU, 0, 0, 0)
            keybd_event(VK_SNAPSHOT, 0, 0, 0)
            keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0)
            keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)

            Application.DoEvents()
            System.Threading.Thread.Sleep(200) ' To have time to catch the clipboard
            If Not System.Windows.Forms.Clipboard.GetDataObject() Is Nothing Then
                Dim oDataObj As IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
                If oDataObj.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap) Then
                    Dim oImgObj As System.Drawing.Image = oDataObj.GetData(DataFormats.Bitmap, True)
                    Dim sFileName As String = Command()
                    If sFileName <> "" Then
                        oImgObj.Save(sFileName, System.Drawing.Imaging.ImageFormat.Gif)
                    End If
                    System.Windows.Forms.Clipboard.Clear()
                End If
            End If
        Catch ex As Exception
            'Do Nothing
            'MsgBox(ex.Message)
        End Try
        Application.Exit()
    End Sub

End Module
 
Open and run the Console program. Press the Alt-Print Screen key combination. This will copy the image to the clipboard. Then you can paste the image into an application such as WORD or Paint, which can be saved to a file. You can edit or crop the image in the file before saving it.
 
I haven't used in a console app but here is how I've screen shot a form and saved as a jpg:
VB.NET:
Imports System.Drawing.Imaging
dim bmpScreenShot as Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)

Dim ScreenShop as Graphics = Graphics.FromImage(bmpScreenShot)

ScreenShot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
ScreenShot.Save("File Path etc .jpg", ImageFormat.Jpeg)

Replace File Path in the ScreenShot.Save line with the complete file path and file name.
I assume this could be used with a console ap but I don't know how to assign the width and height:lookaroundb:
 
Last edited:
Back
Top