0

I am trying to compare two images by using picture box, but I got a problem: How can I pass the selected picture name as a parameter to a function as a string?

I save picture path and name as a string name1 and string name2, but I got a problem when I pass them as parameters.

Below is my code. Please tell me where I am wrong.

private void pictureBox1_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd1 = new OpenFileDialog();
    ofd1.Title = "Select User Profile Image";
    ofd1.Filter = "Image File(*.png;*.jpg;*.bmp;*.gif)|*.png;*.jpg;*.bmp;*.gif";
    if (ofd1.ShowDialog() == DialogResult.OK)
    {
        pictureBox1.Image = new Bitmap(ofd1.FileName);
        string name1 = ofd1.FileName;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    Compare(name1,name2);
}

public void Compare(string bmp1, string bmp2, byte threshold = 3)
{
    Bitmap firstBmp = (Bitmap)Image.FromFile(bmp1);
    Bitmap secondBmp = (Bitmap)Image.FromFile(bmp2);
    firstBmp.GetDifferenceImage(secondBmp, true);
    string result = string.Format("Difference: {0:0.0} %", firstBmp.PercentageDifference(secondBmp, threshold) * 100);
}
2
  • "but i got a problem when i pass as a parameter " What exactly is the nature of your problem? Commented May 20, 2016 at 9:31
  • name1 and name2( there is no name2 field in your code) are not accessable in button1_Click() make them global Commented May 20, 2016 at 9:32

3 Answers 3

4

You create variable name1 inside if statement inside pictureBox1_Click(). You should create class level variable to use it inside button1_Click(), because name1 is visible only inside if block:

public YourClass
{    
    string name1 = String.Empty:

    //..... your code

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd1 = new OpenFileDialog();
        ofd1.Title = "Select User Profile Image";
        ofd1.Filter = "Image File(*.png;*.jpg;*.bmp;*.gif)|*.png;*.jpg;*.bmp;*.gif";
        if (ofd1.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = new Bitmap(ofd1.FileName);
            name1 = ofd1.FileName;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
       Compare(name1,name2);
    }

    public void Compare(string bmp1, string bmp2, byte threshold = 3)
    {
        Bitmap firstBmp = (Bitmap)Image.FromFile(bmp1);
        Bitmap secondBmp = (Bitmap)Image.FromFile(bmp2);
        firstBmp.GetDifferenceImage(secondBmp, true);
        string result = string.Format("Difference: {0:0.0} %", firstBmp.PercentageDifference(secondBmp, threshold) * 100);
    }
}

If you create name2 the same way, you should make it class level variable too.

Sign up to request clarification or add additional context in comments.

2 Comments

For absolute completeness its probably worth checking name1 (and name2) for an empty string before trying to use them to load an image.
Jamiec how to load an image?
0

You can declare a member variable in your Form to save the file path:

public partial class YourForm : Form
{
    private string _imagePath1;
    private string _imagePath2;

   private void pictureBox1_Click(object sender, EventArgs e)
   {
        OpenFileDialog ofd1 = new OpenFileDialog();
        // ... your code
        if (ofd1.ShowDialog() == DialogResult.OK)
        {
             pictureBox1.Image = new Bitmap(ofd1.FileName);
             // SAVE PATH TO CLASS MEMBER
             _imagePath1 = ofd1.FileName;
        }
   }
   private void button1_Click(object sender, EventArgs e)
   {
        // USE CLASS MEMBERS
        Compare(_imagePath1, _imagePath2);
   }
}       

4 Comments

maybe someone thinks you copied his/her answer :P :D
or, maybe someone doesnt like your destruction of naming conventions! (Even the OP got it right)
Though naming conventions would never be a reason for me to downvote an otherwise correct answer, I updated them. Camelcased by accident...
@RenéVogt - I agree. Whoever it was obviously felt guilty :)
0

I think below URL is helpful to you

http://www.c-sharpcorner.com/uploadfile/prathore/image-comparison-using-C-Sharp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.