Several problems in VB service

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Posts: 1
Joined: 2017-07-13T07:35:52-07:00
Authentication code: 1151

Several problems in VB service

Post by mansnes »

Hi there,

I have written a Windows Service that utilizes the file system watcher on a folder that gets a ton of images uploaded to it. When it gets notified of a file being put in the directory (or any subdirectory), it spawns a task to check the file to see its current quality setting, creates a new, temporary file based on the original file, sets that file's quality to 70, saves the temp file and finally, writes the temp file over the original file.

I'm having a couple problems with this. First, it looks like IM is crashing in my call to get the information about the original file randomly sometimes (don't have a reproducible case yet, but I'm trying to isolate - it seems to happen fairly frequently) and I can't catch the exception:

Code: Select all

Application: PixMonService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.SEHException
   at ImageMagick.MagickImage+NativeMethods+X86.MagickImage_ReadFile(IntPtr, IntPtr ByRef)
   at ImageMagick.MagickImage+NativeMagickImage.ReadFile(ImageMagick.MagickSettings)
   at ImageMagick.MagickImage.Read(System.String, ImageMagick.MagickReadSettings, Boolean)
   at ImageMagick.MagickImage..ctor(System.String)
   at PixMonService.PixMonService.ShrinkPicture(System.String)
   at PixMonService.PixMonService+_Closure$__25-0._Lambda$__0()
   at System.Threading.Tasks.Task`1[[System.Boolean, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]].InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   at System.Threading.Tasks.Task.ExecutionContextCallback(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Thread...

Faulting module name: Magick.NET-Q8-x86.Native.dll, version:, time stamp: 0x59465775
Exception code: 0xc0000006
Fault offset: 0x001943f0
Faulting process id: 0x16f4
Faulting application start time: 0x01d2f8315afe6f2c
Faulting application path: C:\Program Files (x86)\path\PixMonService\PixMonService.exe
Faulting module path: C:\Users\administrator\AppData\Local\Temp\Magick.NET.net40.\Magick.NET-Q8-x86.Native.dll
Report Id: 56ccfc1e-65c1-11e7-80cf-b083fec668dd
Secondly, and far worse, is that at random times, the converted file only contains about 1/3 of the image - the rest is grey. I just went through the logs and found that it seems to only be processing these pictures once (I was thinking it might be processing them multiple times). I also thought maybe it was a concurrency issue (especially since I'm not much versed in parallel programming and it has been a long time since I did much coding at all), but I thought the file system would take care of that. Other files in the same folder process fine. There is a significant size difference in the resulting files with the corrupted files typically around 300 KB, the properly compressed files about 1-3 MB and the original files around 5-6 MB.

In case you were wondering why I didn't use a script (I previously was), it is because I'm monitoring from another server than where the files are and the FileSystemWatcher can throw a lot of events quickly and I didn't want to miss them, so it starts a task to do the work so it can get back to monitoring for changes. I am also using a slightly modified FileSystemWatcher that automatically will reconnect in the event of a loss of connectivity to the source to monitor ( ... emwatcher/).

I have posted one each of files that are missing data, successfully compressed, and uncompressed, all from the same folder, as well as the code I'm using in my Dropbox. ... oXgSa?dl=0

If anyone can see what it is that I'm missing and help me out, that would be greatly appreciated. Right now I've got it disabled since yesterday, it corrupted a bunch of files and now I have to go find them and hope there is a backup of them.

Image server and compressing server both Windows Server 2012 R2, up to date with patching
ImageMagick 7.0.6(.100) Q8 32-bit
VisualStudio 2017
The "Tamed FileSystemWatcher" from 7/1/15

Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Several problems in VB service

Post by snibgo »

When do you get notified? Perhaps a file has been created but not yet written to or closed, when you start to read it. Obviously that would cause problems.

IMG_0458.JPG has gray for the bottom two-thirds of rows. Apparently this can happen when IM runs out of resources. The "synchronize" setting might prevent this: see ... ynchronize . I don't know how to so this in VB.

Your output file should be the same as your input file, but at a lower quality setting. So an obvious check is to compare the two images, and check the RMSE score. A score greater than about 2% would suggest there is an error.
snibgo's IM pages:

User avatar
Posts: 1625
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789

Re: Several problems in VB service

Post by dlemstra »

I suspect that you are parsing a file that is not completed yet. I had the same issue inside MagickViewer and I solved it like this: ... ons.cs#L43. I keep trying to access the file until I can exclusively access it. Hope that works for you.
.NET + ImageMagick = Magick.NET, @MagickNET, Donate