user5832488
user5832488

Reputation: 19

How to pass file dialog Instance as a parameter in C#

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);
}

Upvotes: 0

Views: 468

Answers (3)

Sunny Samrat
Sunny Samrat

Reputation: 62

I think below URL is helpful to you

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

Upvotes: 0

Roman
Roman

Reputation: 12201

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.

Upvotes: 4

René Vogt
René Vogt

Reputation: 43936

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);
   }
}       

Upvotes: 0

Related Questions