最小化程序截取屏幕截图

时间:2012-01-13 20:44:38

标签: .net vb.net

我为程序最小化时拍摄的程序编写了一些代码。事情是错误的代码,我不知道它是什么。每当我截取屏幕截图时,程序都会在应用程序中拍摄照片而不是最小化。

所以我需要它做的是最小化应用程序,然后截取屏幕截图,然后在截取屏幕截图后重新打开程序。

对于noob问题我很抱歉,但我是VB的新手,只编写了不到一天的代码。

    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()

任何帮助都会很棒。

3 个答案:

答案 0 :(得分:3)

您在截取屏幕截图后呼叫Hide :)

试试这个:

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()

答案 1 :(得分:0)

在Me.Hide()

之后尝试输入下面的代码
Application.DoEvents()
System.Threading.Thread.Sleep(100)

睡眠100应该足够了,但如果窗口仍然是部分,那么尝试增加更多时间。

修改

看着你的代码,我没有意识到你试图等待。 Thread.Sleep会等你。只需要花费几毫秒的时间。

选项2:

使用BackgroundWorker截取屏幕截图。在启动后台工作程序之前放置Me.Hide,在DoWork中进行屏幕捕获,在RunWorkerCompleted中使用Me.Show。另外,在屏幕截图之前插入DoWork内部System.Threading.Thread.Sleep(100)

答案 2 :(得分:0)

单击按钮时我试过这个。屏幕截图进入名为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

然后单击此按钮将其保存:

    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