|
|
#3
huangjunxing2018-11-28 17:42
回复 2楼 lgwd
你好,我用CopyFromScreen获得截图片,显示在picture控件里面,但是图片刷新的过程太耗时间, 就是这条语句非常耗费时间,picDest.Image = destImg'。差不多总占时间的一半,有什么办法提高速度吗,这是我的代码
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Public Class Form1
Dim ka As Integer
Private Sub GetScreenColor(ByVal IntX1 As Int16, ByVal IntY1 As Int16, ByVal IntX2 As Int16, ByVal IntY2 As Int16)
'创建一个位图
Dim Img As New Bitmap(IntX2 - IntX1 + 1, IntY2 - IntY1 + 1, Imaging.PixelFormat.Format24bppRgb)
Dim destImg As New Bitmap(IntX2 - IntX1 + 1, IntX2 - IntX1 + 1)
'创建一个画图对象,并载入之前创建的位图
Dim G As Graphics = Graphics.FromImage(Img)
'截屏,前两个参数表示截屏起点,第三和第四为绘制图像的起点。
G.CopyFromScreen(IntX1, IntY1, 0, 0, New Size(IntX2 - IntX1 + 1, IntY2 - IntY1 + 1))
' Me.PictureBox1.Image = CType(Img, Image)
'释放画图对象
G.Dispose()
'从左到右,从上到下
'将截取到的图片载入内存
Dim sourceData As Imaging.BitmapData = Img.LockBits(New Rectangle(0, 0, Img.Width, Img.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format24bppRgb)
Dim destData As BitmapData = destImg.LockBits(New Rectangle(0, 0, Img.Width, Img.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb)
'获取首地址
Dim pSource As IntPtr = sourceData.Scan0
'计算字节总数
Dim allBytes As Int32 = Math.Abs(sourceData.Stride) * Img.Height
'存放颜色值的字节数组
Dim rgbvalues(allBytes - 1) As Byte
'从内存中读取字节数据
Marshal.Copy(pSource, rgbvalues, 0, allBytes)
Dim pDest As IntPtr = destData.Scan0
Marshal.Copy(rgbvalues, 0, pDest, allBytes)
'解锁
Img.UnlockBits(sourceData)
destImg.UnlockBits(destData)
Img.Dispose()
picDest.Image = destImg'显示图片的picture控件
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
GetScreenColor(0, 0, 500, 500)
ka = ka + 1
If ka = 500 Then'循环截屏500次
Timer1.Enabled = False
End If
TextBox1.Text = ka
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Enabled = True
End Sub
End Class
|