views:

637

answers:

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

In the above code, if I try to set bi.CreateOptions, it is not taking it. It is showing as none. Please can you suggest a solution?

+2  A: 

Try setting the BitmapImage with Begin/EndInit

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

The MSDN documentation of the BitmapImage constructor you used has a short remark that states that "BitmapImage objects created using this constructor are automatically initialized. After initialization, property changes are ignored."

Therefore, setting the CreateOptions after calling the constructor does not have any effect. Actually, the BitmapImage is being created in the constructor, so it kind of makes sense. You could argue that the property setter should maybe throw an InvalidOperationException.

The solution to your problem is using another constructor, e.g. as it is shown in the example on the MSDN documentation page for the CreateOptions property.

EFrank