我在一个有多个画布和很多按钮的 WPF 应用程序上工作。用户可以加载图像来更改按钮背景。
这是我在 BitmapImage 对象中加载图像的代码
bmp = new BitmapImage();
bmp.BeginInit();
bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.UriSource = new Uri(relativeUri, UriKind.Relative);
bmp.EndInit();
并且在 EndInit() 应用程序的内存增长非常多。
使思考更好(但并不能真正解决问题)的一件事是添加
bmp.DecodePixelWidth = 1024;
1024 - 我的最大画布尺寸。但我应该只对宽度大于 1024 的图像执行此操作 - 那么如何获得 EndInit() 之前的宽度?