Alex
Alex

Reputation: 184

Take a screenshot with program minimized

I wrote some code for a program I am making to take a screenshot when the program is minimized. Thing is something is wrong with the code and I am not sure what it is. Whenever I take a screenshot the program is taking the picture with the application up and not minimized.

So what I need it to do is minimize the application then take the screenshot and then reopen the program after its taken the screenshot.

Sorry for the noob question but I am brand new to the VB and only been coding it in for less then a day.

    Dim screenImage As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
    Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenImage)
    g.CopyFromScreen(New Point(0, 0), New Point(0, 0), New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height))


    Me.Hide()
    screenImage.Save("C:\screenImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    Dim tSpan As TimeSpan = TimeSpan.FromSeconds(System.DateTime.Now.Second)
    Dim tSpan2 As TimeSpan = TimeSpan.FromSeconds(System.DateTime.Now.Second)
    Do While Math.Abs(tSpan.Subtract(tSpan2).Seconds) < 2
        tSpan2 = TimeSpan.FromSeconds(System.DateTime.Now.Second)

    Loop
    Me.Show()

Any help would be great.

Upvotes: 2

Views: 1135

Answers (3)

TGamer
TGamer

Reputation: 529

I have tried this when a button is clicked. the screenshot gets into a picturebox called picDisplay.

    Me.Hide()
    Dim bounds As Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    bounds = Screen.PrimaryScreen.Bounds
    screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    picDisplay.Image = screenshot
    Me.Show()
    'this takes a screenshot

and then clicking this button saves it:

    Dim savefiledialog1 As New SaveFileDialog
    savefiledialog1.Title = "Save File"
    savefiledialog1.FileName = "*.bmp"
    savefiledialog1.Filter = "Bitmap |*.bmp"
    If savefiledialog1.ShowDialog() = DialogResult.OK Then
        picDisplay.Image.Save(savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
    End If
    'and this saves the screenshot

Upvotes: 0

Roman Starkov
Roman Starkov

Reputation: 61512

You are calling Hide after you've taken the screenshot :)

Try this:

Dim screenImage As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenImage)

Me.Hide()
Me.Application.DoEvents() ' <-- might not be necessary; try without it first.

g.CopyFromScreen(New Point(0, 0), New Point(0, 0), New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height))

screenImage.Save("C:\screenImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Dim tSpan As TimeSpan = TimeSpan.FromSeconds(System.DateTime.Now.Second)
Dim tSpan2 As TimeSpan = TimeSpan.FromSeconds(System.DateTime.Now.Second)
Do While Math.Abs(tSpan.Subtract(tSpan2).Seconds) < 2
    tSpan2 = TimeSpan.FromSeconds(System.DateTime.Now.Second)

Loop
Me.Show()

Upvotes: 3

Nick O
Nick O

Reputation: 3826

Try putting in the code below after you Me.Hide()

Application.DoEvents()
System.Threading.Thread.Sleep(100)

The sleep 100 should be enough but if the window is still partially there try adding more time.

Edit:

Looking at you code I didn't realize you tried to put in a wait. The Thread.Sleep will do the wait for you. Just put in the time in milliseconds.

Option 2:

Use a BackgroundWorker to take the screenshot. Just put the Me.Hide before starting the background worker, screen capture in the DoWork and the Me.Show in the RunWorkerCompleted. Also, put in a System.Threading.Thread.Sleep(100) inside of the DoWork before the screenshot.

Upvotes: 0

Related Questions