将图像设置为图像源时覆盖(重新保存)图像时出现问题

发布于 2024-08-10 18:33:27 字数 1021 浏览 1 评论 0原文

大家好,

我在图像权限方面遇到了一些问题。

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

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

    imgAsset.Source = new BitmapImage(uriSource);

这工作正常,如果用户随后立即选择另一个图像并尝试将其保存到原始文件上,则会出现麻烦。

保存图像时生成异常 “ExternalException:GDI+ 中发生一般错误。”

经过一番尝试后,我将错误范围缩小到 imgAsset.Source = new BitmapImage(uriSource); 因为删除这一行并且不设置图像源将允许我多次覆盖该文件。

我还尝试将源设置为其他内容,然后重新保存,希望旧的引用能够被处理,但事实并非如此。

我怎样才能克服这个错误?

谢谢, Kohan

编辑

现在使用此代码我没有收到异常,但图像源没有更新。另外,由于我没有使用 SourceStream,所以我不确定需要处理什么才能使其正常工作。

       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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一向肩并 2024-08-17 18:33:27

你快到了。

  • 使用 BitmapCacheOption.OnLoad 是防止文件被锁定的最佳解决方案。

    使用

  • 要使其每次重新读取文件,您还需要添加 BitmapCreateOptions.IgnoreImageCache。

在代码中添加一行应该可以做到这一点:

  imgTemp.CreateOption = BitmapCreateOptions.IgnoreImageCache;

从而产生以下代码:

  uriSource = new Uri(Combine(imagesDirectoryTemp, generatedFileName), UriKind.Absolute);
  imgTemp = new BitmapImage();
  imgTemp.BeginInit();
  imgTemp.CacheOption = BitmapCacheOption.OnLoad;
  imgTemp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
  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.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
  imgTemp.UriSource = uriSource;
  imgTemp.EndInit();
  imgAsset.Source = imgTemp;
无尽的现实 2024-08-17 18:33:27

当您在任何 WPF 控件中加载图像时,它会处理您的图像,并且在您关闭应用程序之前不会释放它。这样做的原因...我不太清楚,可能是在幕后转发一些 DirectX 代码,这些代码永远不知道 WPF 应用程序何时释放图像。
使用此代码加载图像..

        MemoryStream mstream = new MemoryStream();
        System.Drawing.Bitmap bitmap = new Bitmap(imgName);
        bitmap.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
        bitmap.Dispose(); // Releases the file.

        mstream.Position = 0;

        image.BeginInit();
        image.StreamSource = mstream;
        image.EndInit();
        this.img.Source = image ;   

它对我有用..

When you load an Image in any WPF control it let's handle to your image and doesn't release it until you close your application. The reason for this... I dont know exactly, problably is relaying on some DirectX code behind the scene which never knows when the WPF application releases the image..
Use this code to load the image..

        MemoryStream mstream = new MemoryStream();
        System.Drawing.Bitmap bitmap = new Bitmap(imgName);
        bitmap.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
        bitmap.Dispose(); // Releases the file.

        mstream.Position = 0;

        image.BeginInit();
        image.StreamSource = mstream;
        image.EndInit();
        this.img.Source = image ;   

it worked for me..

2024-08-17 18:33:27

听起来非常像我开发 Intuipic 时遇到的问题,其中 WPF 不会处理图像,从而锁定文件。查看我编写的这个转换器,用于处理问题。

Sounds very much like the problem I had developing Intuipic, where WPF would not dispose of the image, thus locking the file. Check out this converter I wrote to deal with the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文