Zwei Bilder in Stücke aufteilen und Vergleichen z.b gut für Bild übertragung

    • VB.NET

      Zwei Bilder in Stücke aufteilen und Vergleichen z.b gut für Bild übertragung

      Hallo,

      Mit diesen Code kann man 2Bilder Vergleichen z.b (Einen Screenshot und ein äteres Desktop Bild)

      VB.NET-Quellcode

      1. Imports System.Drawing.Drawing2D
      2. Imports System.Drawing
      3. Imports System.Drawing.Imaging
      4. Imports System.Runtime
      5. Public Class Main
      6. Private rect As Rectangle
      7. Private rect2 As Rectangle
      8. Public Sub CheckImages(ByVal SourceImage As Image, ByVal TargetImage As Image, ByVal Part_width As Integer, ByVal Part_height As Integer)
      9. Dim i2 As Integer = 0
      10. For i As Integer = 0 To 10
      11. Dim CurrentBlock As Image = GetScreenRectangle(i, i2, SourceImage)
      12. rect2 = New Rectangle(i * Part_height, i2 * Part_width, Part_height, Part_width)
      13. Application.DoEvents()
      14. If Not modBitMapsEqual.IsEqual(CurrentBlock, GetScreenRectangle(i, i2, TargetImage)) = True Then
      15. ''IMAGE TEIL IST NICHT GLEICH!!!
      16. '' HIER EINÜGEN WAS PASSIEREN SOLL!
      17. End If
      18. If i = 10 AndAlso Not i2 = 10 Then
      19. i2 += 1
      20. i = 0
      21. End If
      22. Next
      23. End Sub
      24. Private Function GetScreenRectangle(ByVal xPos As Integer, ByVal yPos As Integer, ByVal SourceImage As Image)
      25. Dim Region As New Rectangle(xPos * 0, yPos * 0, 0, 0) ''BITTE HIER EURE GRÖßE EINTRAGEN Z,B ( xPos * 100, yPos * 100, 100 ,100)
      26. Return GetPicturePart(SourceImage, Region)
      27. End Function
      28. Private Function CaptureScreen() As Bitmap
      29. Dim b As New Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height)
      30. Dim g As Graphics = Graphics.FromImage(b)
      31. g.CopyFromScreen(0, 0, 0, 0, b.Size)
      32. g.Dispose()
      33. Return b
      34. End Function
      35. Private Function GetPicturePart(ByVal SourceImage As Image, ByVal Region As Rectangle) As Bitmap
      36. Dim ImagePart As Bitmap = New Bitmap(Region.Width, Region.Height)
      37. Using G As Graphics = Graphics.FromImage(ImagePart)
      38. Dim TargetRect As Rectangle = New Rectangle(0, 0, Region.Width, Region.Height)
      39. Dim SourceRect As Rectangle = Region
      40. G.DrawImage(SourceImage, TargetRect, SourceRect, GraphicsUnit.Pixel)
      41. End Using
      42. Return ImagePart
      43. End Function
      44. End Class


      Und das Module:

      VB.NET-Quellcode

      1. Option Strict On
      2. Option Explicit On
      3. Option Infer Off
      4. Imports System
      5. Imports System.Drawing ' Bitmap / Rectangle
      6. Imports System.Drawing.Imaging ' BitmapData / ImageLockMode
      7. Imports System.Runtime ' CompilerServices / InterOpServices
      8. Module modBitMapsEqual
      9. ''' <summary>
      10. ''' Ist in beiden Bitmap-Objekten das gleiche Bild enthalten?
      11. ''' (Kriterium: Alle Pixelwerte sind identisch)</summary>
      12. ''' <param name="bmp1">Erste Bitmap für Vergleich</param>
      13. ''' <param name="bmp2">Zweite Bitmap für Vergleich</param>
      14. ''' <returns>True, falls gleiches Bild, sonst False</returns>
      15. <CompilerServices.Extension()> _
      16. Public Function IsEqual(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As Boolean
      17. Dim equal As Boolean = True ' für Pixelvergleich
      18. ' Sind zwei Bilder vorhanden?
      19. If bmp1 Is Nothing Or bmp2 Is Nothing Then Return False
      20. ' Gleiche Klassen-Instanz ---> gleiches Bild
      21. If Bitmap.ReferenceEquals(bmp1, bmp2) Then Return True
      22. ' Ungleiche Größe/Farbtiefe --> ungleiches Bild
      23. With bmp1
      24. If .Width <> bmp2.Width Then Return False
      25. If .Height <> bmp2.Height Then Return False
      26. If .PixelFormat <> bmp2.PixelFormat Then Return False
      27. End With
      28. ' Bei beiden Bitmaps alle Bild-Daten im Speicher sperren
      29. Dim rect As New Rectangle(0, 0, bmp1.Width, bmp1.Height)
      30. Dim bd1 As BitmapData = bmp1.LockBits(rect, ImageLockMode.ReadOnly, bmp1.PixelFormat)
      31. Dim bd2 As BitmapData = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat)
      32. ' Gesamtzahl der Bild-Bytes per Scanbreite & Bildhöhe ermitteln
      33. Dim ByteZahl As Integer = bd1.Stride * bd1.Height
      34. ' Bitmap-Daten besorgen (Bytearray)
      35. Dim bmp1_bytes(ByteZahl - 1), bmp2_bytes(ByteZahl - 1) As Byte
      36. ' Die gesperrten Bilddaten in 2 Bytearrays kopieren
      37. InteropServices.Marshal.Copy(bd1.Scan0, bmp1_bytes, 0, ByteZahl)
      38. InteropServices.Marshal.Copy(bd2.Scan0, bmp2_bytes, 0, ByteZahl)
      39. ' Bitmap-Daten vergleichen
      40. For i As Integer = 0 To ByteZahl - 1
      41. If bmp1_bytes(i) <> bmp2_bytes(i) Then
      42. equal = False : Exit For
      43. End If
      44. Next i
      45. ' Daten und Ressourcen freigeben
      46. bmp1.UnlockBits(bd1) : bmp2.UnlockBits(bd2)
      47. ' Rückgabe
      48. Return equal
      49. End Function
      50. End Module


      damit kann man dan z.b für eine Bildübertragung benutzen ohne das ganze bild zuschicken.

      mfg vbler.

      Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „vbler“ ()