简体   繁体   English

WPF矩形填充图像刷未更新

[英]WPF Rectangle FIll imagebrush not updating

I am developing a WPF application, a messenger client. 我正在开发WPF应用程序,即Messenger客户端。 The user should be able to change his avatar image. 用户应该能够更改他的头像图像。 When he right-clicks his avatar, a open file dialog appears and there he can select the image he wants. 当他右键单击头像时,会出现一个打开的文件对话框,在那里他可以选择所需的图像。 After he has made his decision I delete his current avatar and copy the new one in the same place with the same name. 在他做出决定后,我删除他当前的化身,并将新的化身复制到相同名称的同一个地方。 The avatar image is a rectangle with an imagebrush fill. 化身图像是一个带有画笔填充的矩形。 The problem is that the image does not update until I restart the application. 问题是,直到我重新启动应用程序,映像才会更新。 Here is the piece of code used to load the new image. 这是用于加载新图像的代码。

if (x.ShowDialog() == true)
{
    ImageBrush img = new ImageBrush();
    BitmapImage bit = new BitmapImage();
    Bitmap bmp = new Bitmap(x.FileName);
    System.IO.File.Delete(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png");
    bmp.Save(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png", System.Drawing.Imaging.ImageFormat.Png);

    bit.BeginInit();
    bit.CacheOption = BitmapCacheOption.OnLoad;
    bit.UriSource = new Uri(@"pack://siteoforigin:,,,/data/images/avatar/x.png");
    bit.EndInit();
    img.ImageSource = bit;
    ProfileImage.Fill = img;
}

I hope you can help me solve this issue or offer me some alternatives. 希望您能帮助我解决此问题或为我提供其他选择。 Thank you all! 谢谢你们!

In order to make BitmapImage reload the image file (with identical file path), you could set the BitmapCreateOptions.IgnoreImageCache option: 为了使BitmapImage重新加载图像文件(具有相同的文件路径),可以设置BitmapCreateOptions.IgnoreImageCache选项:

bit.BeginInit();
bit.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bit.CacheOption = BitmapCacheOption.OnLoad;
bit.UriSource = new Uri(@"pack://siteoforigin:,,,/data/images/avatar/x.png");
bit.EndInit();
img.ImageSource = bit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM