使用纯net技术实现抓屏(包含连续抓图和鼠标的问题的解决方法)

之前转载了一篇关于api抓屏的实例,本次载的是老外Jason Hensley写的抓屏,实现方式与上次不同,可以方便大家对比

''开发平台 VB.NET 2005 <Beta 2>

 ''Description: A basic example of using the new built-in Sub in the
 '2.0 version of the dotNetFramework to capture the screen, or portion
 'of the screen and putting the image in a picturebox. Then you have
 'the option of saving the picturebox image to a JPEG file. This example
 'just uses the basic SourceCopy raster on the screenshot. There
 'are many raster-operations you can choose from.

''Update: Reworked the code to be compatible for VB.NET <Beta 2>. I
 'also added a feature to hide the program when the screenshot is being
 'taken.
 '代码如下:
 Public Class frmMain

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click

        'Create the bitmap and graphics objects.
         Dim b As Bitmap = New Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
         Dim g As Graphics = Graphics.FromImage(b)

        Dim s As Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)

        'See whether or not to hide the program before taking the
         'screenshot.
         If chkHide.Checked Then

            Me.Hide()
             '
         
    'This is a short, sweet, and simple timeout code to pause
             'for a specified amount of seconds. On my computer, 1 sec
             'is more than enough time for my app to be out of the way
             'to take the screenshot to keep it from capturing the app
             'with it.
             Dim t As DateTime = Now.AddSeconds(1)
             '
             Do : Application.DoEvents()
             Loop Until Now.Second >= t.Second
           
  '
             'Copy the image from the screen. You can customize the size
             'and postions to take the screenshot from.
             g.CopyFromScreen(0, 0, 0, 0, s, CopyPixelOperation.SourceCopy)
             '
             'Show the app again after the screenshot has been taken.
             Me.Show()

            'Clear this variable from memory.
             t = Nothing

        Else

            'Copy the image from the screen. You can customize the size
             'and postions to take the screenshot from.
             g.CopyFromScreen(0, 0, 0, 0, s, CopyPixelOperation.SourceCopy)

        End If

        'Load the copied image to the picturebox control.
         pic.Image = b

        'Clean up alittle.
         g.Dispose()
         b = Nothing

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim saveDlg As SaveFileDialog = New SaveFileDialog
         Dim dlgResult As DialogResult = New DialogResult

        'Set the filter to JPEG files only.
         saveDlg.Filter = "JPEG Files (*.jpg)|*.jpg"
         saveDlg.Title = "Select the file to save the image too."

        dlgResult = saveDlg.ShowDialog

        If dlgResult = Windows.Forms.DialogResult.OK Then

            'Save the image contained in the picturebox.
             pic.Image.Save(saveDlg.FileName)

        End If

    End Sub

End Class


我的补充:

1)如果想连续抓屏,就必须将下面的定义部分改为全局变量,以防止反复定义新对象而导致系统崩溃

         Dim b As Bitmap= New Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
         Dim g As Graphics = Graphics.FromImage(b)

        Dim s As Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)

2)抓鼠标的问题,必须获取鼠标位置,然后绘制鼠标到图像

      g.CopyFromScreen(SecScreen_x, SecScreen_y, d_x, d_y, Copy_size, CopyPixelOperation.SourceCopy)
       '添加鼠标位置
     Cursor.Current.Draw(g, New Rectangle(Cursor.Position, Cursor.Current.Size))




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值