Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Load & Comparing Graphic Files

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Resolved [RESOLVED] [2005] Load & Comparing Graphic Files

    Two questions:

    1) How do you load a graphic file into a variable in VB? I assumed you would use an Image variable, by how do you load it? I want to load jpg files.

    2) How do you compare two graphic files? If I can load the files into Image variables I can use either .equals or .gethashcode methods (and then compare the hashcodes). Is this the best way or is there another?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Load & Comparing Graphic Files

    1. Image.FromFile

    2. Comparing Image objects and comparing images are completely different. The Image.Equals method simply tells you whether two variables refer to the same Image object. If you've called Image.FromFile twice then Equals must return False, even if you called FromFile twice on the same file. If you want to compare image files then you will have to do so by comparing each pixel of the two images. Of course, that's assuming that they're both the same dimensions, which they must be to be the same.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: [2005] Load & Comparing Graphic Files

    Image.fromfile seems to work (atleast no errors). I tried comparing hashcodes, and that also does not work, even loading the same file (as you indicated for .equals). So much for the easy way of doing it.

    Can you give me any pointers as to how to do the pixel by pixel comparison? I could not find where to get the pixel data. BTW, does this approach work for both jpg and bmp file formats?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: [2005] Load & Comparing Graphic Files

    I figured it out. The loaded images had to be loaded as Bitmaps.

    vb Code:
    1. Dim img, imgFile As Bitmap
    2.  
    3.  img = Image.FromFile(strTemp.Trim & ".jpg", True)

    Then I wrote a function that compared the two images.

    vb Code:
    1. Function CompareImages(ByVal imgA As Bitmap, ByVal imgB As Bitmap) As Boolean
    2.         Dim WidthA, WidthB, HeightA, HeightB As Integer
    3.         Dim i, j As Integer
    4.         Dim flag As Boolean
    5.  
    6.         widthA = imgA.Width
    7.         widthB = imgB.Width
    8.         heightA = imgA.Height
    9.         HeightB = imgB.Height
    10.  
    11.         flag = True
    12.         If WidthA = WidthB AndAlso HeightA = HeightB Then
    13.             For i = 0 To WidthA - 1
    14.                 For j = 0 To HeightB - 1
    15.                     If imgA.GetPixel(i, j) <> imgB.GetPixel(i, j) Then
    16.                         flag = False
    17.                         i = WidthA
    18.                         j = HeightA
    19.                     End If
    20.                 Next
    21.             Next
    22.         Else
    23.             flag = False
    24.         End If
    25.  
    26.         CompareImages = flag
    27.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width