Windows Service: Using BitmapEncoder or BitmapDecoder Ends "Success Completed"

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:

+5
source share
1 answer

Successful operation

This cryptic message narrows the exact code in the HwndWrapper constructor, which fails. WPF has an error in the declaration of GetStockObject . The SetLastError = true property is incorrect; GetStockObject () does not actually generate an error code. You see a description of error code 0, "nothing worked."

GetStockObject () is a winapi function that never fails if it receives the correct argument. Stock objects are pre-allocated and never issued. You have very strong evidence that the state of the process is completely damaged. Seeing the β€œHeap Allocation Error” message in the event log is certainly part of this misfortune.

If you do not know what could cause this damage, the machine is known - well with reliable RAM, you do not run any dangerous native code, and the machine does not have any other services that could damage a bunch of the desktop, then the only alternative you have is to create a minidump of the crashed process. Contact Microsoft Support, they can track the trace from GetStockObject () failure. Beware that you will have to go through the first levels of support, those that will tell you about the exchange of the car :)

+2
source

Source: https://habr.com/ru/post/1238827/


All Articles