Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像绘制代码从WinForm移植到WPF_C#_Wpf_Winforms_Reference_System.drawing - Fatal编程技术网

C# 将图像绘制代码从WinForm移植到WPF

C# 将图像绘制代码从WinForm移植到WPF,c#,wpf,winforms,reference,system.drawing,C#,Wpf,Winforms,Reference,System.drawing,我在WinForm应用程序中做了一些原型设计,代码对我很有用: using System.Drawing; using System.Drawing.Imaging; Image img0 = null; img0 = Image.FromFile(filename); PropertyItem[] propItems = img0.PropertyItems; 然而,当我将它复制到WPF应用程序时,我得到了错误和引用之间的不兼容 我试图添加.NET引用System.Drawing和Sys

我在WinForm应用程序中做了一些原型设计,代码对我很有用:

using System.Drawing;
using System.Drawing.Imaging;

Image img0 = null;
img0 = Image.FromFile(filename);
PropertyItem[] propItems = img0.PropertyItems;
然而,当我将它复制到WPF应用程序时,我得到了错误和引用之间的不兼容

我试图添加.NET引用System.Drawing和System.Drawing.Imaging,但与System.Windows.Control.Image存在冲突。
任何人都可以建议如何重写上述代码以使其在WPF中工作。

每当您发现自己在处理类名冲突时,您可以设置一个using来指定在使用该类时要使用的名称空间:

using System.Drawing;
using System.Drawing.Imaging;
using Image = System.Drawing.Image;
或者,可以在声明中显式定义命名空间:

System.Drawing.Image img0 = null;
img0 = System.Drawing.Image.FromFile(filename);
我个人更喜欢第一种方法,如果我知道我不会同时使用Image(System.Drawing和System.Windows.Controls)类的话

现在,如果您试图以编程方式创建System.Windows.Controls.Image,请选中:

我倾向于说“从头开始”,而不是将Winform代码移植到WPF。除非你管理(并且想要)这样做。如果它是一个原型,我同意Fildor,请用WPF从头开始重写它。如果你复制并粘贴到你的实际应用程序中,它从来都不是原型,但会带来原型代码通常存在的所有问题。重写这三行没有问题,但我找不到等价的代码,我通过提供文件名加载文件,并立即访问属性项。请建议WPF中Image.FromFile和PropertyItem的等效项。另一个选项是将Image.FromFile替换为等效项,但System.Windows.Control中的Image与System.Drawing中的Image不同,因此它没有FromFile方法。WinForm代码是获取PropertyItems值的一种简单方法。您到底想实现什么?您正在尝试创建动态图像控件吗?如果是这样,则需要使用新的BitmapImage对象设置System.Windows.Controls.Image的ImageSource属性BitmapImage没有PropertyItems。注意:不要使用
Image.FromFile(path)
。它只是
Bitmap
的构造函数,作为其不太具体的
Image
超类返回。没有理由不使用新位图(路径)。@Joe所以你真正的问题是“如何使用WPF读取图像元数据”。