WPF 背景换肤

来源:互联网 发布:淘宝主机哪家好 编辑:程序博客网 时间:2024/05/02 19:25

最近做了个小项目,用到实时换背景,遇到的问题解决,代码:

                    ImageBrush imageBrush = new ImageBrush();
                    BitmapImage bi = new BitmapImage();
                    bi.BeginInit();
                    bi.UriSource = new Uri(file, UriKind.Absolute);

                    //
                    bi.CacheOption = BitmapCacheOption.OnLoad;

                    //以下代码必须有,忽略系统缓冲,否则背景有时在缓冲的影响下,替换不了
                    bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    bi.EndInit();
                    imageBrush.ImageSource = bi; 
                    imageBrush.Stretch = Stretch.Fill;
                    this.Background = imageBrush ;

0 0
原创粉丝点击