This project has moved. For the latest updates, please go here.

MagickImage.Write throws MagickCoderWarningException

Apr 14, 2014 at 7:43 PM
I am reading two PNGs into two MagickImage instances, calling Merge on a MagickImageCollection of them, and then calling Write on the merged image.

Usually this works fine, but with one pair of images, Write throws a MagickCoderWarningException ("Magick: Not recognizing known sRGB profile that has been edited `...' @ warning/png.c/MagickPNGWarningHandler/1832").

The file is created, and since it is a warning, I trust that the file is correct.

However, if it is just a warning, it seems to me that Write should return a MagickWarningException, like Read, rather than throw it. Otherwise every call to Write needs to be wrapped in a try/catch block, which is the workaround I will use for now.

P.S. Thanks for this great library!
Coordinator
Apr 14, 2014 at 8:36 PM
I made some changes a while ago to make sure Warnings are no longer thrown and raised as an event (MagickImage.Warning). It seems I missed the Write operation. This will be fixed in the next release of Magick.NET
Apr 14, 2014 at 9:15 PM
That's great; thanks!
Apr 22, 2014 at 1:38 PM
If I come across this situation as well, should i simply just put my Write into a try/catch in order to get around this? Or is there a more elegant way I should handle this?

Thanks.
Coordinator
Apr 22, 2014 at 1:54 PM
I will publish a new release this week that fixes this problem. You will have to use a try catch for now.
Apr 22, 2014 at 1:54 PM
Works for me. Thanks
Apr 22, 2014 at 8:32 PM
For me, even after adding try/catch,the file is not actually being created. I'm only working with file, not two and just doing a normal conversion. I'm using a try/catch on my side but the file is not being created properly. Is this something that will be corrected in the next release?

My current pseudo-code is this:
  1. Create ImageMagick object #1 using source path ( a .tiff file)
  2. Change format to be different (.pdf)
  3. Call .Write(stream) on object #1. THIS WORKS FINE.
  4. Create ImageMagick object #2 from resulting open stream
  5. Grab color profile from object #1 and put it on object #2
  6. call .Write(stream) on object #2. It's on this write where the warning/error occurs. My try/catch is around this guy.
The resulting image is 0 bytes. Right format, but 0 bytes.
Coordinator
Apr 22, 2014 at 8:51 PM
What kind of exception is being raised? Only warnings can be ignored. My fix only makes sure that warnings are ignored during the Write operation.
Apr 22, 2014 at 9:33 PM
It's the exact same warning that opened this thread. I just know that my situation is slightly different than the issue that opened this thread. I'm not merging two images. I'm just trying to take one image, convert, copy color profile, and then write. I just wanted to confirm that my issue would be covered by your fix. It sounds like it would. But just wanted to double check.




Apr 22, 2014 at 9:37 PM
And the other difference is that the original issues creates a file, whereas my file is created but with zero bytes.

Apr 23, 2014 at 12:57 AM
Here's the image/code in question. Keep in mind that this code is really spread across a few functions, so there are some things that might not make sense, mainly re-creating the source MagickImage object twice as well as setting the stream back to zero length twice. They actually make sense within the context of my overall code. But the below should show you the warning I'm getting and the resulting .png file with zero bytes.


Image: https://www.dropbox.com/s/vfli58sxzmywcy3/22525_008_0030_CC.tif

Code:
         var destinationStream = new FileStream("test.png", FileMode.Create);

        //this is the image in dropbox
        string sourceFilePath = @"C:\22525_008_0030_CC.tif";

        MagickImage magickImage1 = new MagickImage(sourceFilePath);

        magickImage1.Strip();
        magickImage1.Format = MagickFormat.Png;
        magickImage1.Density = new MagickGeometry(300, 300);

        destinationStream.SetLength(0);
        magickImage1.Write(destinationStream);

        MagickImage magickImageDestination;

        destinationStream.Seek(0, SeekOrigin.Begin);
        magickImageDestination = new MagickImage(destinationStream);

        MagickImage magickImageSource = new MagickImage(sourceFilePath);

        ColorProfile cp = magickImageSource.GetColorProfile();

        if (cp != null)
        {
            magickImageDestination.AddProfile(cp);
        }

        destinationStream.SetLength(0);

        try
        {
            magickImageDestination.Write(destinationStream);
        }
        catch (Exception e)
        {

        }
Coordinator
Apr 23, 2014 at 9:02 AM
I did a quick test with the old version of the code and you are correct. The try/catch 'fix' does not work when you write to a stream. It does work when you write to a file name instead of a stream. This is the difference between both operations:

Write(stream)
  1. Create memory blob.
  2. Tell ImageMagick to write to the memory blob.
  3. Check for exception and raise if it occurred.
  4. Copy memory blob to stream.
Write(fileName)
  1. Tell ImageMagick to write to the file.
  2. Check for exception and raise if it occurred.
Feel free to contact me through CodePlex if this is really urgent for you. I could send you a development version (please specify platform Q8/Q16).
Apr 23, 2014 at 10:13 AM
It is a rather urgent need for us, and thanks. The platform is Q16. Just let me know what you would like me to do. I'm not sure what you mean by contact you through CodePlex other than this.

Thanks again.
Coordinator
Apr 23, 2014 at 10:48 AM
There is a contact button on this page: https://www.codeplex.com/site/users/view/dlemstra. It will give me your e-mail address so I can reply with a dll that includes your fix. And with platform I mean x86/x64 or are you using the AnyCPU version of Magick.NET?
Apr 23, 2014 at 12:04 PM
Cool. Just did it while specifying Q16-x64. Thanks. Just let me know if you need me to do anything else.