Picturebox et Drag'n drop
Bonjour,
J'ai un petit souci pour comparer le contenu de 2 PictureBox avec le drag'n'drop.
En fait, lorsque je glisse l'image de la pictureBox1 sur l'image de la pictureBox2 , j'aimerais pouvoir déterminer si ce sont les mêmes ou non.
J'ai essayé de comparer avec la propriété Tag mais en vain .
Quelqu'un aurait une idée pour pouvoir réaliser cela ? £
Merci d'avance!
comparer 2 images,meme contenu
bonjour
ca implique de comparer les 2 images pixel par pixel.GDI+ le permet mais ca peut prendre des heures de calcul (fonction getpixel).
L'autre methode infaillible c'est d'utiliser le hashcode d'une donnee calcule par un algo de cryptage (System.Security.Cryptography) qui calcule un hascode pour chacune des donnees à comparer.
Voici le dragdrop d'un picturebox et une classe ComparingImages dedie au calcul du hashcode du picturebox1.image "source" et du picturebox2.image "cible".
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
Public Class Form2
Dim bmp1 As Bitmap
Dim bmp2 As Bitmap
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.AllowDrop = True
bmp2 = My.Resources.Nénuphars
bmp1 = My.Resources.Collines
PictureBox1.Image = bmp1
PictureBox2.Image = bmp2
End Sub
Private Sub Form2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
Me.PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub
Private Sub Form2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Form2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim x As Integer = Me.PointToClient(New Point(e.X, e.Y)).X
Dim y As Integer = Me.PointToClient(New Point(e.X, e.Y)).Y
If (x >= PictureBox2.Location.X And x <= PictureBox2.Location.X + PictureBox2.Width _
And y >= PictureBox2.Location.Y And y <= PictureBox2.Location.Y + PictureBox2.Height) Then
'Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
'PictureBox1.Image = Image.FromFile(files(0))
Dim nn As Bitmap = e.Data.GetData(DataFormats.Bitmap)
If ComparingImages.Compare(PictureBox2.Image, e.Data.GetData(DataFormats.Bitmap)) Then
PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
End If
End If
End Sub
End Class
'classe à mettre dans un fichier classe à part dans le meme projet
'si les 2 images n'ont pas la meme taille inutile d'aller plus loin
'sinon on calcule le hashcode et comparison
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Security.Cryptography
Public Class ComparingImages
Public Enum CompareResult
ciCompareOk
ciPixelMismatch
ciSizeMismatch
End Enum
Public Shared Function Compare(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As CompareResult
Dim cr As CompareResult = CompareResult.ciCompareOk
'Test pour voir si meme taille image
If (bmp1.Size <> bmp2.Size) Then
cr = CompareResult.ciSizeMismatch
Else
'Convertir chaque image en byte array
Dim ic As System.Drawing.ImageConverter = New System.Drawing.ImageConverter()
Dim btImage1() As Byte = New Byte(1) {}
btImage1 = CType(ic.ConvertTo(bmp1, btImage1.GetType()), Byte())
Dim btImage2() As Byte = New Byte(1) {}
btImage2 = CType(ic.ConvertTo(bmp2, btImage2.GetType()), Byte())
'Cacule hash pour chaque image
Dim shaM As SHA256Managed = New SHA256Managed()
Dim hash1() As Byte = shaM.ComputeHash(btImage1)
Dim hash2() As Byte = shaM.ComputeHash(btImage2)
'Compare les valeurs hash
For i As Integer = 0 To (i < hash1.Length And i < hash2.Length And _
(cr = CompareResult.ciCompareOk))
If hash1(i) <> hash2(i) Then
cr = CompareResult.ciPixelMismatch
End If
Next
End If
Return cr
End Function
End Class |
bon code.....