加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:26:37 所属栏目:百科 来源:网络整理
导读:全天好, 我有图像权限的麻烦. 我从文件加载图像,调整大小,然后将其保存到另一个文件夹. 我正在这样显示: uriSource = new Uri(Combine(imagesDirectoryTemp,generatedFileName),UriKind.Absolute); imgAsset.Source = new BitmapImage(uriSource); 这是正常
全天好,

我有图像权限的麻烦.

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

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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读