I tried hashing, but that did not work. How would I do this?
Thanks.

Recommended Answers

All 5 Replies

If you are want to compare the content of the two files, you could use the following

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            // here must be code to check the number and types of arguments
            // assumed are args[1] is  full path tofirst file, args[2] to second file

            using (StreamReader str1 = new StreamReader(args[1]))
            {
                using (StreamReader str2 = new StreamReader(args[2]))
                {
                    if (str1.ReadToEnd() == str2.ReadToEnd())
                    {
                        Console.WriteLine("The files {1} and{2} are equal", args[1], args[2]);
                    }
                    else
                    {
                        Console.WriteLine("The files {1} and{2} are not equal", args[1], args[2]);
                    }
                }
            }
        }
    }
}

Thanks for the reply. I used the get pixel method, and have the code as below:

var g = true;
if (clipimage.Size == i.Size)
{
    for (int x = 0; x < clipimage.Width; x++)
    {
        for (int y = 0; y < clipimage.Height; y++)
        {
            if (((Bitmap)clipimage).GetPixel(x, y) == ((Bitmap)i).GetPixel(x, y))
            {
                g = false;
                break;
            }
        }
        if (g == false)
            break;
    }
}

At the end, you can check whether g is true. Is this more efficient than the previous method? This one only compares the images if they are the same dimensions, and stops reading pixels as soon as one is found to be different.
Thanks.

The GetPixel() method is better. Because it won't read all the pixels if there's a difference while the other method will always read both the files.

@farooqaa

GetPixel is not better. For it to work the image must be read completely as well. For how else do you fill a clipimage? And the image processing introduces a lot of overhead. Furthermore: my method is filetype independent (works on docs etc as well).

On one point I agree.
It can be sped up by adding a test to the filesize (see below)

using System;
using System.IO;

namespace ConsoleApplication2
{
class Program
{
static int foundequal1 = 0;
static int foundunequal1 = 0;

static void Main(string[] args)
{
DateTime starttimt1 = DateTime.Now;
Console.WriteLine("Start method1 at {0}", starttimt1);
string[] filenames = Directory.GetFiles("m:\\Foto\'s\\vakantie foto's\\200602 Cuba", "*.jpg", SearchOption.AllDirectories);
for (int i = 0; i < filenames.Length; i++)
{
for (int j = 0; j < filenames.Length; j++)
{
compareMethod1(filenames[i], filenames[j]);
}
}
DateTime endtimt1 = DateTime.Now;
Console.WriteLine("End method1 at {0}. Unequal compares: {1}. Equal compares: {2}.", endtimt1, foundunequal1, foundequal1);
}

static void compareMethod1(string file1, string file2)
{
using (StreamReader str1 = new StreamReader(file1))
{
using (StreamReader str2 = new StreamReader(file2))
{
if (str1.BaseStream.Length == str2.BaseStream.Length && str1.ReadToEnd() == str2.ReadToEnd())
{
foundequal1++;
}
else
{
foundunequal1++;
}
}
}
}
}
}

runs in less then a minute. the folder contains 152 jpg files all about 2 mbytes in size and is placed on a slow (5400rpm) external disk attached through a usb2 interface.

since the code for the getpixel example is not complete I could not compare, but if you can make it complate (including preparing clipimmage etc) I am happy to do the compare.

Well, both the images are under an image array - not being loaded from a file.
I'll elaborate,
The program monitors the clipboard for changes, and if an image is added, it must not be identical to others. clipimage is the clipboard image, and i is an image that is saved in the memory.
So, there was a bug in my program. I revised it,

var g = true;
foreach (Image i in himages)
{
    var a = true;
    if (i != null)
    {
        if (clipimage.Size == i.Size)
        {
            for (int x = 0; x < clipimage.Width; x++)
            {
                for (int y = 0; y < clipimage.Height; y++)
                {
                    if (((Bitmap)clipimage).GetPixel(x, y) != ((Bitmap)i).GetPixel(x, y))
                    {
                        a = false;
                        break;
                    }
                }
                if (a == false)
                    break;
            }
        }
        else
           a = false;
    }
    else
        a = false;
    if (a == true)
    {
        g = false;
        break;
    }
}

It now works. g is the variable that will tell me whether the images are the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.