【问题标题】:How to update image file binding to Image control?如何更新图像文件绑定到图像控件?
【发布时间】:2011-07-20 03:53:22
【问题描述】:

我的应用程序包含一个绑定到磁盘映像文件的图像控件。我有一些条件,图像文件需要更新。但是无法进行更新,因为图像文件已打开且无法覆盖。我该怎么办?

【问题讨论】:

    标签: wpf file binding overwrite


    【解决方案1】:

    您可以尝试删除绑定,这样您的程序就不会使用该图像 而不是覆盖图像文件 然后重新添加绑定

    我不确定,但值得一试

    【讨论】:

    • 它不起作用。我试过BindingOperations.ClearBinding(); img.Source = null;GC.Collect(),但图像仍然是写保护的。
    【解决方案2】:

    现在我的解决方案是: 使用转换器将图像路径转换为 ​​BitmapImage。 在转换器中,使用 FileStream 加载图像并将数据复制到 MemoryStream 中,最后关闭 FileStream。

            BitmapImage bmp = new BitmapImage();
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.BeginInit();
            var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            var memStream = new MemoryStream();
            memStream.SetLength(fileStream.Length);
            fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
            memStream.Flush();
            fileStream.Close();
            bmp.StreamSource = memStream;
            bmp.EndInit();
            return bmp;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2017-03-07
      相关资源
      最近更新 更多