Click here to Skip to main content
15,917,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to load a Image with OFD and it does not work. Here is the C# code:


It gives me an error in this line if (op.ShowDialog() == true) The error is Operator '==' cannot be applied to operands of type 'DialogResult' and 'bool'.
Please help.

What I have tried:

private void Button_add_Click(object sender, RoutedEventArgs e) {
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "Select a picture";
            op.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            if (op.ShowDialog() == true)
            {
                Image1.Source = new BitmapImage(new Uri(op.FileName));
            }

        }
Posted
Updated 30-May-17 3:18am

 
Share this answer
 
Like Richard pointed you to, READ THE DOCUMENATION!

The ShowDialog() method doesn't return a boolean. It returns a DialogResult enum value.

You could have solved this yourself in seconds had you just read the documentation to see what ShowDialog() returns.
 
Share this answer
 
There are two different versions of OpenFileDialog.

One belongs to the namespace System.Windows.Forms.
https://msdn.microsoft.com/en-us/library/e61ft40c(v=vs.110).aspx

The other one belongs to the namespace Microsoft.Win32.
https://msdn.microsoft.com/en-us/library/ms614336(v=vs.110).aspx

Have a look at the using session on top of your source code.
I am sure you have referenced System.Windows.Forms.

In order to compare the return value of ShowDialog to a nullable bool
you must replace
using System.Windows.Forms
by
using Microsoft.Win32

Make sure you added the corresponding reference.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900