c# – 将图像设置为图像源时重写(重新保存)图像时出现问题

前端之家收集整理的这篇文章主要介绍了c# – 将图像设置为图像源时重写(重新保存)图像时出现问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
全天好,

我有图像权限的麻烦.

我从文件加载图像,调整大小,然后将其保存到另一个文件夹.
我正在这样显示

uriSource = new Uri(Combine(imagesDirectoryTemp,generatedFileName),UriKind.Absolute);

    imgAsset.Source = new BitmapImage(uriSource);

这是正常的,如果用户然后立即选择另一个图像并尝试将其保存在原始文件之后,问题就会发生.

保存我的图像时会生成异常“ExternalException:GDI发生一般错误”.

经过一些玩过之后,我把错误缩小到了imgAsset.Source = new BitmapImage(uriSource);因为删除此行并且不设置imagesource将允许我多次覆盖此文件.

我也试图将来源设置为别的东西,在重新保存之前,希望旧的参考将被处理,但情况并非如此.

我怎么能超过这个错误

谢谢,
可汗

编辑

现在使用这个代码我没有得到例外,但图像源没有更新.另外,由于我没有使用SourceStream,我不知道我需要处理什么来让这个工作.

uriSource = new Uri(Combine(imagesDirectoryTemp,UriKind.Absolute);

       imgTemp = new BitmapImage();
       imgTemp.BeginInit();
       imgTemp.CacheOption = BitmapCacheOption.OnLoad;
       imgTemp.UriSource = uriSource;
       imgTemp.EndInit();

       imgAsset.Source = imgTemp;

解决方法

你几乎在那里

>使用BitmapCacheOption.OnLoad是保护文件被锁定的最佳方案.
>要使它重新读取文件,每次你还需要添加BitmapCreateOptions.IgnoreImageCache.

在你的代码添加一行应该做到这一点:

imgTemp.CreateOption = BitmapCreateOptions.IgnoreImageCache;

从而导致此代码

uriSource = new Uri(Combine(imagesDirectoryTemp,UriKind.Absolute);
  imgTemp = new BitmapImage();
  imgTemp.BeginInit();
  imgTemp.CacheOption = BitmapCacheOption.OnLoad;
  imgTemp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
  imgTemp.UriSource = uriSource;
  imgTemp.EndInit();
  imgAsset.Source = imgTemp;
原文链接:https://www.f2er.com/csharp/96218.html

猜你在找的C#相关文章