Hallo.
Ich möchte eine optimale Bildschirmübertragung entwickeln, das heißt, sie soll sehr effizient sein (vergleichbar zu
Teamviewer Bildschirmübertragung).
Dazu benötige ich eure Tipps.
Derzeit bin ich am vergleichenn der Screens, denn es sollen ja nur die
Pixel übertragen werden, die sich verändert haben.
-Parallel.For ist bei meinem Test langsamer gewesen als das normale For , wie kommt das?
-Was habt ihr generell zum Code zu sagen? Bitte um Verbesserungsvorschläge!
Ich möchte eine optimale Bildschirmübertragung entwickeln, das heißt, sie soll sehr effizient sein (vergleichbar zu
Teamviewer Bildschirmübertragung).
Dazu benötige ich eure Tipps.
Derzeit bin ich am vergleichenn der Screens, denn es sollen ja nur die
Pixel übertragen werden, die sich verändert haben.
-Parallel.For ist bei meinem Test langsamer gewesen als das normale For , wie kommt das?
-Was habt ihr generell zum Code zu sagen? Bitte um Verbesserungsvorschläge!
VB.NET-Quellcode
- Option Strict On
- Option Infer Off
- Imports System.Runtime.InteropServices
- Public Class Form1
- Private CurrScreenVar As Bitmap
- Private Property CurrScreen() As Bitmap
- Get
- Return CurrScreenVar
- End Get
- Set(value As Bitmap)
- If value.Equals(CurrScreenVar) Then Exit Property
- CurrScreenVar = value
- End Set
- End Property
- Private Function TakeScreen() As Bitmap
- Dim S As Screen = Screen.PrimaryScreen
- Dim B As Bitmap = New Bitmap(S.Bounds.Width, S.Bounds.Height)
- Dim G As Graphics = Graphics.FromImage(B)
- G.CopyFromScreen(S.Bounds.Left, S.Bounds.Top, 0, 0, S.Bounds.Size)
- Return B
- End Function
- Private Structure BitmapChangeData
- Public Sub New(L As Integer, Vall As Short)
- AbsoluteIndex = L
- Valuee = Vall
- End Sub
- Public AbsoluteIndex As Long
- Public Valuee As Short
- End Structure
- Private Recto, RectoTwo As Rectangle
- Private BiD As New BitmapChangeData
- Private Pointz As New List(Of BitmapChangeData)
- Private Function CompareScreens(bmp As Bitmap, old As Bitmap) As BitmapChangeData()
- If Not bmp.Size.Equals(old.Size) Then Return New BitmapChangeData() {}
- Pointz.Clear()
- Recto.Size = New Size(bmp.Width, bmp.Height)
- RectoTwo.Size = New Size(old.Width, old.Height)
- Dim bmpdat As Imaging.BitmapData = bmp.LockBits(Recto, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)
- Dim bmpdatTwo As Imaging.BitmapData = old.LockBits(RectoTwo, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)
- Dim ptr As IntPtr = bmpdat.Scan0
- Dim ptrTwo As IntPtr = bmpdatTwo.Scan0
- Dim bytes As Integer = Math.Abs(bmpdat.Stride) * bmp.Height
- Dim bytestwo As Integer = Math.Abs(bmpdatTwo.Stride) * old.Height
- Dim Rgbz(bytes - 1) As Byte
- Dim RgbzTwo(bytestwo - 1) As Byte
- Marshal.Copy(ptr, Rgbz, 0, bytes)
- Marshal.Copy(ptrTwo, RgbzTwo, 0, bytestwo)
- 'Parallel.For(0, Rgbz.Length - 1, Sub(i)
- ' If Not Rgbz(i) = RgbzTwo(i) Then
- ' BiD.AbsoluteIndex = i
- ' BiD.Valuee = Rgbz(i)
- ' Pointz.Add(BiD)
- ' End If
- ' End Sub)
- For i As Integer = 0 To Rgbz.Length - 1
- If Not Rgbz(i) = RgbzTwo(i) Then
- BiD.AbsoluteIndex = i
- BiD.Valuee = Rgbz(i)
- Pointz.Add(BiD)
- End If
- Next
- Marshal.Copy(Rgbz, 0, ptr, bytes)
- Marshal.Copy(RgbzTwo, 0, ptrTwo, bytestwo)
- bmp.UnlockBits(bmpdat)
- old.UnlockBits(bmpdatTwo)
- Return Pointz.ToArray
- End Function
- Private Async Sub Doit()
- Await Task.Run(Sub()
- If CurrScreen Is Nothing Then
- CurrScreen = TakeScreen()
- Me.Invoke(Sub() Me.Text = "First screenshot.")
- Else
- Dim st As New Stopwatch
- st.Start()
- Dim k As Bitmap = TakeScreen()
- Me.Invoke(Sub() Me.Text = "Veränderte Pixel: " & CompareScreens(k, CurrScreen).Count.ToString)
- st.Stop()
- MessageBox.Show(st.Elapsed.Milliseconds.ToString)
- End If
- End Sub)
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Doit()
- End Sub
- End Class