Image processing in .NET!! RRS feed

  • Question

  • User2069745655 posted
    Hi guys,
    I got a prob.I need to upload two images on a picturebox or image button,and compare both for if there are any changes and say that images vary.Normally we use Image processing techniques ,algorithms.But how to work it out in asp.net(vb) or vb.net forms.Are there any classes or name spaces for this in .NET 2003 or we use Hash functions or r there any image converters!!Kindly suggest me pl!!!
    Many thanks for any sites links!!!

    Sunday, December 4, 2005 11:49 PM

All replies

  • User1550782130 posted

    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[1];
       bImage1 = (byte[])thisConverter.ConvertTo(Image1, bImage1.GetType());
       byte[] bImage2 = new byte[1];
       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";

    Monday, December 5, 2005 12:01 PM
  • User2069745655 posted
    Hi man,

      U really did a great job!! i am really thankful to u.But one prob is,if I use same images it is showing error that "image1 is already in use".So I tried saving the same image in different path".The result is same...

                  I got to work this application out in vb.net (window form and webform.aspx.vb form).Can  I.Secondly can I show the difference (hash numbers--the difference).Please help me out.

    Wednesday, December 7, 2005 12:45 AM
  • User1550782130 posted

    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.

    Thursday, December 8, 2005 6:20 PM
  • User2069745655 posted
    Hi man,
    I am sorry actually.Why I asked for hash code in labels, is just show their differences.My project is all about two images to be compared in the form, say two tiger image paws, draw lines b/n two points  of the images, calculate their finger areas and area on whole and check some other parameters to confirm the differences b/n two images.I hope u understand my prob...
    Friday, December 9, 2005 2:40 AM
  • User1550782130 posted

    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.

    Friday, December 9, 2005 3:30 PM
  • User2128074059 posted

    Hi bharathreddyd,

      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.

    Saturday, December 10, 2005 12:18 AM
  • User-1673888866 posted
    Hi Jun,
    Its Swami again. Please tell me how to to gain a destination image which show the difference between the source two images.


    Saturday, December 24, 2005 12:09 AM
  • User2128074059 posted

    Merry Christmas!

    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.

    Saturday, December 24, 2005 9:58 PM