Last post Dec 24, 2005 09:58 PM by jeepzj
Dec 04, 2005 11:49 PM|bharathreddyd|LINK
Dec 05, 2005 12:01 PM|Cal Zant|LINK
I have created some example code to show you how this can be done in .NET. Right now it is in C#, because that is the language that I use primarily ... but I will try to post the VB.NET equivalent later. It will probably look
very similiar. Hopefully this will at least give you somewhere to start.
This example has 2 file inputs on the page, a label to display the output of the comparison, and a button. Here is what the front-end looks like:
<title>Image Comparer Example</title>
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
<input type=file id="File1" runat=server ><br>
<input type=file id="File2" runat=server ><br>
<asp:Label ID="lblOutput" Runat=server /><br><br>
<asp:Button ID="btnCompare" Text="Compare" runat=server />
Then in the btnCompare click event you would have the following code which uploads the files to the server, compares them, and displays the output of the comparison (NOTE: You will have to include the System.Security.Cryptography namespace, and will
have to change the path of the where it stores the files on the server):
// Upload the files to the server
string tempPath1 = Server.MapPath("/TestApp/Photos/" + System.IO.Path.GetFileName(File1.Value));
string tempPath2 = Server.MapPath("/TestApp/Photos/" + System.IO.Path.GetFileName(File2.Value));
// Create a Bitmap objects
Bitmap Image1 = new Bitmap(tempPath1);
Bitmap Image2 = new Bitmap(tempPath2);
bool Different = false;
// Do a quick check to make sure they are the same size
if(Image1.Size != Image2.Size)
Different = true;
// Convert each image to a byte array
ImageConverter thisConverter = new System.Drawing.ImageConverter();
byte bImage1 = new byte;
bImage1 = (byte)thisConverter.ConvertTo(Image1, bImage1.GetType());
byte bImage2 = new byte;
bImage2 = (byte)thisConverter.ConvertTo(Image2, bImage2.GetType());
// Compute a hash for each image
SHA256Managed shaM = new SHA256Managed();
byte Hash1 = shaM.ComputeHash(bImage1);
byte Hash2 = shaM.ComputeHash(bImage2);
// Compare the hash values
for (int i = 0; i < Hash1.Length && i < Hash2.Length && !Different; i++)
if (Hash1[i] != Hash2[i])
Different = true;
this.lblOutput.Text = "Different";
this.lblOutput.Text = "Same";
Dec 07, 2005 12:45 AM|bharathreddyd|LINK
Dec 08, 2005 06:20 PM|Cal Zant|LINK
Sorry for the slow reply ... I am REALLY busy so I don't have time right now to sit down and work on some example code to help you out. What I would try first is to dispose of the bitmap objects after you have compared
them. You can do with some code that looks something like this:
That will release them from memory, and might fix the "image1 is already in use" problem you are having by causing it to instantiate a new object everytime.
You can also print out the two hash codes if they are different ... although I don't know how helpful it would be for a user to see the hash values (but then again I don't know what you are trying to do). In the if statement where you catch the difference
(if (Hash1[i] != Hash2[i])), just add some code to set lblOutput.Text to whatever the 2 hash values are. Then change the code at the very bottom to be more like this so it won't over write the output with the "Different" text:
this.lblOutput.Text = "Same";
Hope it helps ... if I get time I will check back to see if you need some more direction on this.
Dec 09, 2005 02:40 AM|bharathreddyd|LINK
Dec 09, 2005 03:30 PM|Cal Zant|LINK
I think if you follow the suggestions I made in the last post, you should be able to get the two hash codes printed into the label. If not, let me know.
If there is a specific part about it you are confused about, or need help with let me know. I don't promise that I have all the answers, but I can try.
Dec 10, 2005 12:18 AM|jeepzj|LINK
I just saw all of your problems. And I guess you want to gain a destination image which show the difference between the source two images. Am I right? If so, maybe I can help.
Dec 24, 2005 12:09 AM|swamik|LINK
Dec 24, 2005 09:58 PM|jeepzj|LINK
You can only compare the pixel A in the first image with the opposite pixel B in the second image.
If A!=B, it means that there is a difference between the two images, and you can put the pixel A to the opposite position as a sign pixel in the destination image.
If A==B, it means the two pixels are the same, and you can put a transparency color to the opposite position in the destination image.
Loop all the pixels in the two images by the two conditions above.
Finally you can get a clear image which show the difference between the source two images.