This project has moved and is read-only. For the latest updates, please go here.

Possible bug with large file streams?

Apr 29, 2014 at 12:25 AM
Using this image: https://www.dropbox.com/s/hvgfu4f6s25zhqu/PreviewforTif.tif. It's large(191 MB)
       var destinationStream = new FileStream("test.pdf", FileMode.Create);

        //this is the image in dropbox
        string sourceFilePath = tifFile;

        MagickImage magickImage1 = new MagickImage(sourceFilePath);

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

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

       //if I stop right here, I get a .pdf at 300 dpi.  Perfect.  but as I continue...

        destinationStream.Seek(0, SeekOrigin.Begin);
        MagickImage 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)
        {

        }
Here I end up with a .pdf that is sometimes corrupted. It's also only like 8 mb, which can't be right. And if it's not corrupted, it's only 72 dpi.

Let me know if you need more information or if I am missing something.

Thanks, Tim
Apr 29, 2014 at 12:18 PM
You need to set the density when you read the PDF that you have just created, otherwise it will use 72 dpi as the default.
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(300, 300);
MagickImage magickImageDestination = new MagickImage(destinationStream, settings);
I would however advise you to not use a PDF file as an intermediate format. This format use the GhostScript program and creates PNG files on disk. You are probably better of to use TIFF or PNG as the intermediate format.
Apr 29, 2014 at 12:35 PM
Shouldn't the density and format already be a part of the destination stream that was created on this line:

magickImage1.Write(destinationStream);

So when I read from that stream, the density and format should carry over?
Apr 29, 2014 at 4:15 PM
I agree that you would expect the density to be maintained. But the PDF reader currently does not support that. I found this example on how to parse it: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=16598. I will see when I can find some time to add this for you.
Apr 29, 2014 at 9:41 PM
Edited Apr 29, 2014 at 9:44 PM
You're code snippet above seems to work for pdf's. But I'm having a similar problem converting to .png. They all end up being 72 DPI.

Using this image: https://www.dropbox.com/s/k7khbpvbrqqtryz/22307_009_%20JM_0055.jpg
    var destinationStream = new FileStream("test.png", FileMode.Create);

    //this is the image in dropbox
    string sourceFilePath = jpgFile;

    MagickImage magickImage1 = new MagickImage(sourceFilePath);

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

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

    destinationStream.Seek(0, SeekOrigin.Begin);
    MagickReadSettings settings = new MagickReadSettings();
    settings.Density = new MagickGeometry(300, 300);
    settings.Format = MagickFormat.Png;
    MagickImage magickImageDestination = new MagickImage(destinationStream, settings);

    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)
    {

    }
Apr 29, 2014 at 10:47 PM
The PNG reader only reads the density when the ResolutionUnits are set.
magickImage1.ResolutionUnits = Resolution.PixelsPerInch;
And there is a bug when you read magickImageDestination.ResolutionUnits. It will show PixelsPerInch because you set it to that. But the value will always be PixelsPerCentimeter for a PNG image. The next release will return the correct value.
Apr 30, 2014 at 12:27 AM
Edited Apr 30, 2014 at 12:28 AM
Thanks. And just to clarify. In the following code, using the same source file as above(https://www.dropbox.com/s/k7khbpvbrqqtryz/22307_009_%20JM_0055.jpg):
        var destinationStream= new FileStream("JPGtoPNG300test.png", FileMode.Create)
        MagickImage magickImage = new MagickImage(sourceFilePath);
        magickImage.Strip();

        magickImage.Format = MagickFormat.Png;
        if (magickImage.Format == MagickFormat.Png)
        {
            magickImage.ResolutionUnits = Resolution.PixelsPerInch;
        }

        //set DPI
        magickImage.Density = new MagickGeometry(300, 300);

        destinationStream.SetLength(0);
        magickImage.Write(destinationStream);
The final result still shows 72 DPI. Did your response mean that it will continue to show 72 DPI until the next release? or is the bug something different?
Apr 30, 2014 at 6:03 AM
I ran my tests without magickImage.Strip();. If you remove that it will work. But I have no idea why that is happening. I will have to look in the PNG coder and figure that out for you.

I would advise you to remove the if and always set the ResolutionUnits to PixelsPerInch.
Apr 30, 2014 at 1:37 PM
Thanks. Do you happen to know when the next full release will be that covers some of the recent changes?
Apr 30, 2014 at 2:29 PM
I was planning to publish a release last weekend but I did not have enough time to do it. I hope I can publish the release this weekend.