Hallo,
Mit diesen Code kann man 2Bilder Vergleichen z.b (Einen Screenshot und ein äteres Desktop Bild)
Und das Module:
damit kann man dan z.b für eine Bildübertragung benutzen ohne das ganze bild zuschicken.
mfg vbler.
Mit diesen Code kann man 2Bilder Vergleichen z.b (Einen Screenshot und ein äteres Desktop Bild)
VB.NET-Quellcode
- Imports System.Drawing.Drawing2D
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Imports System.Runtime
- Public Class Main
- Private rect As Rectangle
- Private rect2 As Rectangle
- Public Sub CheckImages(ByVal SourceImage As Image, ByVal TargetImage As Image, ByVal Part_width As Integer, ByVal Part_height As Integer)
- Dim i2 As Integer = 0
- For i As Integer = 0 To 10
- Dim CurrentBlock As Image = GetScreenRectangle(i, i2, SourceImage)
- rect2 = New Rectangle(i * Part_height, i2 * Part_width, Part_height, Part_width)
- Application.DoEvents()
- If Not modBitMapsEqual.IsEqual(CurrentBlock, GetScreenRectangle(i, i2, TargetImage)) = True Then
- ''IMAGE TEIL IST NICHT GLEICH!!!
- '' HIER EINÜGEN WAS PASSIEREN SOLL!
- End If
- If i = 10 AndAlso Not i2 = 10 Then
- i2 += 1
- i = 0
- End If
- Next
- End Sub
- Private Function GetScreenRectangle(ByVal xPos As Integer, ByVal yPos As Integer, ByVal SourceImage As Image)
- 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)
- Return GetPicturePart(SourceImage, Region)
- End Function
- Private Function CaptureScreen() As Bitmap
- Dim b As New Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height)
- Dim g As Graphics = Graphics.FromImage(b)
- g.CopyFromScreen(0, 0, 0, 0, b.Size)
- g.Dispose()
- Return b
- End Function
- Private Function GetPicturePart(ByVal SourceImage As Image, ByVal Region As Rectangle) As Bitmap
- Dim ImagePart As Bitmap = New Bitmap(Region.Width, Region.Height)
- Using G As Graphics = Graphics.FromImage(ImagePart)
- Dim TargetRect As Rectangle = New Rectangle(0, 0, Region.Width, Region.Height)
- Dim SourceRect As Rectangle = Region
- G.DrawImage(SourceImage, TargetRect, SourceRect, GraphicsUnit.Pixel)
- End Using
- Return ImagePart
- End Function
- End Class
Und das Module:
VB.NET-Quellcode
- Option Strict On
- Option Explicit On
- Option Infer Off
- Imports System
- Imports System.Drawing ' Bitmap / Rectangle
- Imports System.Drawing.Imaging ' BitmapData / ImageLockMode
- Imports System.Runtime ' CompilerServices / InterOpServices
- Module modBitMapsEqual
- ''' <summary>
- ''' Ist in beiden Bitmap-Objekten das gleiche Bild enthalten?
- ''' (Kriterium: Alle Pixelwerte sind identisch)</summary>
- ''' <param name="bmp1">Erste Bitmap für Vergleich</param>
- ''' <param name="bmp2">Zweite Bitmap für Vergleich</param>
- ''' <returns>True, falls gleiches Bild, sonst False</returns>
- <CompilerServices.Extension()> _
- Public Function IsEqual(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As Boolean
- Dim equal As Boolean = True ' für Pixelvergleich
- ' Sind zwei Bilder vorhanden?
- If bmp1 Is Nothing Or bmp2 Is Nothing Then Return False
- ' Gleiche Klassen-Instanz ---> gleiches Bild
- If Bitmap.ReferenceEquals(bmp1, bmp2) Then Return True
- ' Ungleiche Größe/Farbtiefe --> ungleiches Bild
- With bmp1
- If .Width <> bmp2.Width Then Return False
- If .Height <> bmp2.Height Then Return False
- If .PixelFormat <> bmp2.PixelFormat Then Return False
- End With
- ' Bei beiden Bitmaps alle Bild-Daten im Speicher sperren
- Dim rect As New Rectangle(0, 0, bmp1.Width, bmp1.Height)
- Dim bd1 As BitmapData = bmp1.LockBits(rect, ImageLockMode.ReadOnly, bmp1.PixelFormat)
- Dim bd2 As BitmapData = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat)
- ' Gesamtzahl der Bild-Bytes per Scanbreite & Bildhöhe ermitteln
- Dim ByteZahl As Integer = bd1.Stride * bd1.Height
- ' Bitmap-Daten besorgen (Bytearray)
- Dim bmp1_bytes(ByteZahl - 1), bmp2_bytes(ByteZahl - 1) As Byte
- ' Die gesperrten Bilddaten in 2 Bytearrays kopieren
- InteropServices.Marshal.Copy(bd1.Scan0, bmp1_bytes, 0, ByteZahl)
- InteropServices.Marshal.Copy(bd2.Scan0, bmp2_bytes, 0, ByteZahl)
- ' Bitmap-Daten vergleichen
- For i As Integer = 0 To ByteZahl - 1
- If bmp1_bytes(i) <> bmp2_bytes(i) Then
- equal = False : Exit For
- End If
- Next i
- ' Daten und Ressourcen freigeben
- bmp1.UnlockBits(bd1) : bmp2.UnlockBits(bd2)
- ' Rückgabe
- Return equal
- End Function
- 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“ ()