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

ToBase64 is always different for same image

Mar 5, 2015 at 5:17 PM
I am converting a PDF to image and sending back to client Base 64 encoding string

using (MagickImage pdfConvertedImage = images[index])
                    {
                        // ReSharper restore AccessToDisposedClosure
                        //// set the format of the image
                        pdfConvertedImage.Format = MagickFormat.Png;
                        pdfConvertedImage.Resize(new MagickGeometry(width, height));                    

                        var keyValuePair = new KeyValuePair<string, string>(
                                    string.Format(imageFolderPath + FilePng, index),
                                    pdfConvertedImage.ToBase64());

                        imageCollection.Add(keyValuePair);
}

But, the Base64 string is always different for the Image. For the same resolution, I should expect the image to be exact, ie the base 64 encoded string , should match, but its not happening.

Can you please help me.

I am using
<package id="Magick.NET-Q8-x64" version="7.0.0.0009" targetFramework="net451" />
Coordinator
Mar 5, 2015 at 6:50 PM
A png image contains metadata that will result in a different base64 value. Why do you need to have the same base64 output?
Mar 5, 2015 at 7:33 PM
we are trying to hash the image, using the base64 encoded string, to either send the newly generated image or just send the hash back, to save the network bandwidth, as the image has to go through couple of hops to reach device. to ensure the client renders the image stored on local store on a device, instead of rendering the image.
Coordinator
Mar 5, 2015 at 7:35 PM
Edited Mar 5, 2015 at 7:35 PM
If you need a hash of the image you should use the Signature property of the MagickImage. This creates a hash based on the values of the pixels.
Mar 5, 2015 at 8:25 PM
I do not see a property called as "Signature", can you please elaborate, how to get the hash .
Coordinator
Mar 5, 2015 at 8:30 PM
My bad. The Signature property was added in 7.0.0.0010. I would suggest you to upgrade to the latest version.
Mar 6, 2015 at 9:03 PM
Thanks for your response, I am able to use Signature property get the hash of the Image.

I have another question, when we start processing Image with resolution of 1440 * 1080, , the image conversion is taking nearly 5 seconds. Is there any tweaks to improve the REsizing of Image?
Coordinator
Mar 7, 2015 at 7:49 AM
Edited Mar 7, 2015 at 7:50 AM
5 seconds for resizing an image that small sounds a bit excessive. It might be possible that it is using OpenCL acceleration when it shouldn't. You can disable this with the MagickNET.UseOpenCL property. Are you really sure that the resize part is taking the longest?
Mar 9, 2015 at 5:11 PM
Yes,

Convert to Image TimeTaken = 5263

I am processing the PDF, with the following code, setting the UseOpenCL to false did not help, the processing time is still around 5 seconds

MagickNET.SetGhostscriptDirectory(ghostScriptLocation);
        MagickNET.UseOpenCL = false;
        int width = Convert.ToInt32(displayWidth);
        int height = Convert.ToInt32(displayHeight);
        var settings = new MagickReadSettings { Density = new PointD(defaultDpi) };
        using (var images = new MagickImageCollection())
        {
            //// Add all the pages of the pdf file to the collection
            images.Read(pdfDocumentInBytes, settings);

            ////int page = 1;

            Parallel.For(
                0,
                images.Count,
                delegate(int index)
                {
                    // ReSharper disable AccessToDisposedClosure
                    using (MagickImage pdfConvertedImage = images[index])
                    {
                        // ReSharper restore AccessToDisposedClosure
                        //// set the format of the image
                        pdfConvertedImage.Format = MagickFormat.Png;
                        pdfConvertedImage.Resize(new MagickGeometry(width, height));
                        var keyValuePair = new KeyValuePair<string, string>(
                                    string.Format(imageFolderPath + FilePng, index),
                                    pdfConvertedImage.ToBase64());
                        var imageHashKeyValuePair = new KeyValuePair<string, string>(
                            index.ToString(CultureInfo.InvariantCulture),
                            pdfConvertedImage.Signature);
                             imageCollection.Add(keyValuePair);
                        imageCollection.Add(imageHashKeyValuePair);
                    }
                });
        }
Coordinator
Mar 9, 2015 at 6:55 PM
Edited Mar 9, 2015 at 6:55 PM
It is most likely that reading the PDF file is taking the most time. You might want to decrease the Density to improve the speed. And writing a PNG file can also take a while due to the compression that is used. But changing the density or the output format could lower the quality of your output image.
Mar 9, 2015 at 7:34 PM
thanks for your reply

As you suggested the culprit is reading of PDF file.
Time taken to Read the PDF in milliseconds :3291
Time taken to Resize image in milliseconds :1268

If I get PDF outputted with 600 DPI, can I assume, the resize operation for 853640 and 14401080, maintaining the aspect ration of 4:3, we will not get any degradation in quality of image
IF I change the way to read the PDF to

images.Read(pdfDocumentInBytes); without using settings

instead of
images.Read(pdfDocumentInBytes, settings);
Coordinator
Mar 9, 2015 at 7:47 PM
I would suggest you to use the settings and specify your own density. What is the current value that you are using? And what are you planning to do with the images? Depending on your use case you might only need 150 or 300 dpi.
Mar 10, 2015 at 5:17 PM
I have changed the default DPI to 300

With 600 DPI, I was processing the request in
Width(853) * Height(640) 6.124 seconds
Width(1819) * Height(1364) 9.43


with 300 DPI
Width(853) * Height(640) 3.03
Width(1819) * Height(1364) 4.11

The prcessing times have reduced considerably with this change, I am still testing the image quality on device.

Thanks for your help.