I am having a problem with the fact that I cannot solve any google solution.
I am running a service that downloads or saves images and uses the BitmapEncoder or BitmapDecoder . After some time (depending on how often I save / load images), the service refuses to save / load images. First I see a warning in the event log with
failed heap allocation
I googled what this means and has to do with the limited number of GDI objects that the Windows service has. It can be modified by the registry to increase the number of these objects, but its not a good solution, I think, and also it does not work for me.
My code throws the following exception with a stack trace when saving
Error while storing image : System.ComponentModel.Win32Exception (0x80004005): The operation completed successfully at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks) at System.Windows.Threading.Dispatcher..ctor() at System.Windows.Threading.DispatcherObject..ctor() at System.Windows.Media.Imaging.BitmapEncoder..ctor(Boolean isBuiltIn) at Imaging.TiffReadWrite.Save(String filename, Image img)
and at boot
Error while loading image : System.ComponentModel.Win32Exception (0x80004005): The operation completed successfully at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks) at System.Windows.Threading.Dispatcher..ctor() at System.Windows.Threading.DispatcherObject..ctor() at System.Windows.Media.Imaging.BitmapDecoder..ctor(Stream bitmapStream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, Guid expectedClsId) at Imaging.TiffReadWrite.Load(String filename)
My code for saving images is as follows:
public static void Save(string filename, BitmapSource img) { using (FileStream stream = new FileStream(filename, FileMode.Create)) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.None; BitmapFrame frm = BitmapFrame.Create(img); encoder.Frames.Add(frm); encoder.Save(stream); } }
and for loading images it looks like this:
public static BitmapSource Load(string filename) { BitmapSource resultImage = null; using (Stream imSource = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { var decoder = new TiffBitmapDecoder(imSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); resultImage = decoder.Frames[0]; } return resultImage; }
Thus, the service refuses to save / load images. I can try / catch this exception so that the service can continue to work, but the images could not be saved / loaded. Sometimes after the first occurrence of this exception, it was possible to save / load several images, and after some time the save / load will not be performed.
My only workaround for this problem is not to run this code in the service, but in the application. Then it works fine, but this is not the solution I'm looking for. If anyone has a better suggestion, please let me know.
There are several similar messages (the trace of the exception stack is more or less the same) that are not actually resolved: