0

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

4

2 回答 2

0

现在我的解决方案是:使用转换器将图像路径转换为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;
于 2011-03-22T05:37:27.567 回答
0

您可以尝试删除绑定,因此您的程序不会使用该图像,而是覆盖图像文件并重新添加绑定

我不确定,但值得一试

于 2011-03-22T03:11:37.400 回答