快速比较两副图片是否相同

来源:互联网 发布:免费淘宝客cms 编辑:程序博客网 时间:2024/04/27 21:29
 

转自Codeproject 

http://www.codeproject.com/dotnet/comparingimages.asp

    Public Enum CompareResult

        ciCompareOk

        ciPixelMismatch

        ciSizeMismatch

    End Enum

 

 

    Public Shared Function Compare(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As CompareResult

        '首先检查两副图片大小是否完全相等

        If Not bmp1.Size.Equals(bmp2.Size) Then

            Return CompareResult.ciSizeMismatch

        Else

            '把每个图片转成一字节数组

            Dim ic As New System.Drawing.ImageConverter

            Dim btImage1(1) As Byte

            btImage1 = CType(ic.ConvertTo(bmp1, btImage1.GetType()), Byte())

            Dim btImage2(1) As Byte

            btImage2 = CType(ic.ConvertTo(bmp2, btImage2.GetType()), Byte())

            Debug.WriteLine(UBound(btImage1))

            '计算每个图片的hash值

            Dim shaM As New SHA256Managed

            Dim hash1 As Byte() = shaM.ComputeHash(btImage1)

            Dim hash2 As Byte() = shaM.ComputeHash(btImage2)

            '比较hash值

            Dim i As Integer

            For i = 0 To Math.Min(hash1.Length, hash2.Length) - 1

                If hash1(i) <> hash2(i) Then

                    Return CompareResult.ciPixelMismatch

                End If

            Next

        End If

        Return CompareResult.ciCompareOk

    End Function

原创粉丝点击