Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems overwriting (re-saving) image when it was set as image source

Tags:

c#

image

save

wpf

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;
like image 755
4imble Avatar asked Nov 06 '09 16:11

4imble


2 Answers

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;
like image 78
Ray Burns Avatar answered Sep 18 '22 05:09

Ray Burns


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..

like image 37
jmayor Avatar answered Sep 18 '22 05:09

jmayor