用openfiledialog加载图片。 (WPF) [英] Loading a picture with openfiledialog. (Wpf)

查看:324
本文介绍了用openfiledialog加载图片。 (WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用OFD加载一个图像但它不起作用。这是C#代码:





如果(op.ShowDialog()== true)它在这一行给出了一个错误错误是操作符'=='不能应用于'DialogResult'和'bool'类型的操作数。

请帮助。



< b>我尝试了什么:



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

        }

推荐答案

参见CommonDialog.ShowDialog Method(System.Windows.Forms) [ ^ ]。


就像Richard指出的那样,阅读文档!



ShowDialog()方法不返回布尔值。它返回一个DialogResult枚举值。



如果您刚刚阅读文档以了解ShowDialog()返回的内容,您可以在几秒钟内自行解决。
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.


有两个不同版本的OpenFileDialog。



一个属于命名空间System.Windows.Forms。

< a href => https://msdn.microsoft.com/en-us/library/e61ft40c(v=vs.110).aspx



另一个属于名称空间Microsoft.Win32。

https://msdn.microsoft.com/en-us/library/ms614336( v = vs.110).aspx



查看源代码顶部的使用会话。

我相信你引用了System.Windows.Forms。



为了比较ShowDialog的返回值和可空的布尔值

你必须替换

使用System.Windows.Forms

by

using Microsoft.Win32



Make确定你添加了corres积水参考。
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.


这篇关于用openfiledialog加载图片。 (WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆