C# 如何使用C在WPF中设置BitmapImage.CreateOptions#

C# 如何使用C在WPF中设置BitmapImage.CreateOptions#,c#,wpf,bitmapimage,C#,Wpf,Bitmapimage,在上面的代码中,如果我试图设置bi.CreateOptions,它不会接受它。它显示为无。请提供解决方案?尝试使用Begin/EndInit设置位图图像 BitmapImage bi = new BitmapImage(new Uri(@"D:\DSC_0865.png")); bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat; img1.Source = bi; 尝试使用Begin/EndInit设置位图图像 BitmapI

在上面的代码中,如果我试图设置
bi.CreateOptions
,它不会接受它。它显示为无。请提供解决方案?

尝试使用Begin/EndInit设置位图图像

BitmapImage bi = new BitmapImage(new Uri(@"D:\DSC_0865.png"));
bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
img1.Source = bi;

尝试使用Begin/EndInit设置位图图像

BitmapImage bi = new BitmapImage(new Uri(@"D:\DSC_0865.png"));
bi.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
img1.Source = bi;
有一个简短的备注,说明“使用此构造函数创建的BitmapImage对象将自动初始化。初始化后,将忽略属性更改。”

因此,在调用构造函数后设置CreateOptions没有任何效果。实际上,BitmapImage是在构造函数中创建的,所以它有点道理。您可能会争辩说,属性设置程序可能应该抛出InvalidOperationException

您的问题的解决方案是使用另一个构造函数,如上的示例所示。

有一个简短的备注,说明“使用此构造函数创建的BitmapImage对象将自动初始化。初始化后,将忽略属性更改。”

因此,在调用构造函数后设置CreateOptions没有任何效果。实际上,BitmapImage是在构造函数中创建的,所以它有点道理。您可能会争辩说,属性设置程序可能应该抛出InvalidOperationException

您的问题的解决方案是使用另一个构造函数,如上的示例所示