问题覆盖(重新保存)图像时,它被设置为图像源 [英] Problems overwriting (re-saving) image when it was set as image source

查看:139
本文介绍了问题覆盖(重新保存)图像时,它被设置为图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一天都好,

我有一些麻烦图像的权限。

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

  uriSource =新的URI(联合收割机(imagesDirectoryTemp,generatedFileName),UriKind.Absolute);    imgAsset.Source =新的BitmapImage(uriSource);

这是工作的罚款,麻烦来,如果用户再后立即选择其他的图像,并试图将其保存在原来的文件。

一个例外是在保存我的形象生成的ExternalException:GDI +中发生一般性错误。

一些玩弄我已经缩小误差降低到 imgAsset.Source =新的BitmapImage(uriSource)后; 去掉这一行,而不是设置的ImageSource会让我覆盖此文件多次。

我也试过源设置到别的东西,重新保存,希望旧的引用将被处理之前,这是情况并非如此。

如何才能突破这个错误?

谢谢,
可汗

修改

现在使用这种code我没有得到异常但图像源没有更新。此外,由于我没有使用的SourceStream,林不知道我需要处置得到这个工作。

  uriSource =新的URI(联合收割机(imagesDirectoryTemp,generatedFileName),UriKind.Absolute);       imgTemp =新的BitmapImage();
       imgTemp.BeginInit();
       imgTemp.CacheOption = BitmapCacheOption.OnLoad;
       imgTemp.UriSource = uriSource;
       imgTemp.EndInit();       imgAsset.Source = imgTemp;


解决方案

您快到了。


  • 使用BitmapCacheOption.OnLoad是最好的解决方案,让您的文件被锁定。


  • 要导致它每次你还需要添加BitmapCreateOptions.IgnoreImageCache时间重新读取文件。<​​/ p>


添加一行到你的code应该这样做:

  imgTemp.CreateOption = BitmapCreateOptions.IgnoreImageCache;

从而导致该code:

  uriSource =新的URI(联合收割机(imagesDirectoryTemp,generatedFileName),UriKind.Absolute);
  imgTemp =新的BitmapImage();
  imgTemp.BeginInit();
  imgTemp.CacheOption = BitmapCacheOption.OnLoad;
  imgTemp.CreateOption = BitmapCreateOptions.IgnoreImageCache;
  imgTemp.UriSource = uriSource;
  imgTemp.EndInit();
  imgAsset.Source = imgTemp;

Good day all,

I am having some trouble with image permissions.

I am loading an image from file, resizing it and then saving it out to another folder. I am then displaying this like so:

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

    imgAsset.Source = new BitmapImage(uriSource);

This is working fine, the trouble comes if the user then selects another image immediately after and tries to save it over the original file.

An exception is generated upon saving my image "ExternalException: A generic error occurred in GDI+."

After some playing around i have narrowed the error down to imgAsset.Source = new BitmapImage(uriSource); as removing this line and not setting the imagesource will allow me to overwrite this file many times.

I have also tried setting the source to something else, before re-saving in the hope that the old reference would be disposed, this was not the case.

How can i get past this error?

Thanks, Kohan

Edit

Now using this code i am not getting the exception however the image source is not updating. Also since i am not using a SourceStream, im not sure what i need to dispose of to get this working.

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

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

       imgAsset.Source = imgTemp;

解决方案

You're almost there.

  • Using BitmapCacheOption.OnLoad was the best solution to keep your file from being locked.

  • To cause it to reread the file every time you also need to add BitmapCreateOptions.IgnoreImageCache.

Adding one line to your code should do it:

  imgTemp.CreateOption = BitmapCreateOptions.IgnoreImageCache;

thus resulting in this code:

  uriSource = new Uri(Combine(imagesDirectoryTemp, generatedFileName), UriKind.Absolute);
  imgTemp = new BitmapImage();
  imgTemp.BeginInit();
  imgTemp.CacheOption = BitmapCacheOption.OnLoad;
  imgTemp.CreateOption = BitmapCreateOptions.IgnoreImageCache;
  imgTemp.UriSource = uriSource;
  imgTemp.EndInit();
  imgAsset.Source = imgTemp;

这篇关于问题覆盖(重新保存)图像时,它被设置为图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆