Duane

Duane

  • NA
  • 77
  • 0

Problems with a subroutine to match bitmaps.

Sep 17 2012 5:50 PM
I have developed a sub to match two bitmaps pixel-by-pixel. Bm1 is a newly created bitmap saved to a temporary directory. Bm2 represents the existing bitmaps stored in a permanent directory. The problem is I am getting a Match response for two bitmaps even when I know they are not the same. Can somebody tell me what I am doing wrong? If you need more information, please respond and tell me what you need to know. Thanks.

Private Sub btnMatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMatch.Click
        PatternArrayCreator.BmpTempSave()
        Dim dir As New DirectoryInfo("C:\Documents and Settings\Owner\My Documents\Pentominos\")
        Dim fileArr As FileInfo() = dir.GetFiles("*.bmp")
        Dim fiInfo As FileInfo
        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()

        Dim bm1 As Bitmap = Image.FromFile("C:\bmpt.bmp")
        Dim bm2 As Bitmap = Nothing

        Dim bm3 As Bitmap = New Bitmap(121, 201)

        Dim are_identical As Boolean = True

        For Each fiInfo In fileArr
            bm2 = Image.FromFile(fiInfo.FullName)
            For x As Integer = 0 To bm3.Width - 1
                For y As Integer = 0 To bm3.Height - 1
                    If bm1.GetPixel(x, y).Equals(bm2.GetPixel(x, y)) Then
                        are_identical = True
                    Else
                        are_identical = False
                    End If
                Next
            Next
            If are_identical = False Then
                MsgBox("The images are different.")
                Continue For
            Else
                MsgBox("The images match.")
                Exit Sub
            End If
        Next

        Me.Cursor = Cursors.Default

        bm1.Dispose()
        bm2.Dispose()

    End Sub

Answers (9)