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

Images turn to grain

Aug 19, 2016 at 4:46 PM
Here is the process my user wants.

A customer uploads an image. I must make the image 2.5" x 1.38" due to later requirements.
This may require me to shrink it, enlarge it, or put a white border around it to make it fit properly.
I must handle .PDF, .TIFF, .JPEG, .GIF, etc.
I must allow the user to then crop it down.

I can't seem to get Magick.NET to work. It either turns the image to such high grain that it's not legible, or it turns the image into such a large image that processing is dreadfully slow.

I am currently testing with a 96dpi image, but I am bumping it to 300 dpi to try to avoid the grain problem.

Here is my upload code.
            #region Convert PDF to TIFF
            if (System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".pdf")
            {
                using (MagickImage reformat = new MagickImage(Folder + imageFile.imagename))
                {
                    reformat.CompressionMethod = CompressionMethod.LZW;
                    imageFile.imagename = System.IO.Path.GetFileNameWithoutExtension(imageFile.imagename) + ".tiff";
                    reformat.Write(Folder + imageFile.imagename);
                }
            }
            #endregion

            #region Resample at 300 dpi
            using (MagickImage img = new MagickImage(Folder + imageFile.imagename))
            {
                Boolean changed = false;
                if (img.Quality != 100 || img.Depth != 24)
                {
                    img.Quality = 100;
                    img.Depth = 24;
                    img.Resample(new PointD(300, 300));
                    changed = true;
                }
                if (img.ColorSpace != ColorSpace.sRGB && img.ColorSpace != ColorSpace.RGB)
                {
                    img.AddProfile(ColorProfile.SRGB);
                    img.ColorSpace = ColorSpace.sRGB;
                    img.RePage();
                    changed = true;
                }

                if (changed)
                {
                    imageFile.imagename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(imageFile.imagename);
                    img.Write(Folder + imageFile.imagename);
                }
            }
            #endregion

            #region Get Image close to requirements
            Single MaxWidth = 0;
            Single MaxHeight = 0;
            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Folder + imageFile.imagename))
            {
                imageFile.HR = bmp.HorizontalResolution;
                imageFile.VR = bmp.VerticalResolution;
                MaxWidth = ImageWidth * imageFile.HR;
                MaxHeight = ImageHeight * imageFile.VR;

                Single width = bmp.Width;
                Single height = bmp.Height;

                if (width != MaxWidth)
                {
                    Single ratio = width / MaxWidth;
                    height = height * ratio;
                    width = MaxWidth;
                }
                if (height > MaxHeight)
                {
                    Single ratio = height / MaxHeight;
                    width = width * ratio;
                    height = MaxHeight;
                }
            }
            Single thumbWidth = ThumbWidth;
            Single thumbHeight = ThumbHeight;
            using (MagickImage img = new MagickImage(Folder + imageFile.imagename))
            {
                if (img.Quality != 100 || img.Depth != 24)
                {
                    img.Quality = 100;
                    img.Depth = 24;
                    img.Resample(new PointD(300, 300));
                }
                img.Resize(Convert.ToInt32(Math.Floor(MaxWidth)), Convert.ToInt32(Math.Floor(MaxHeight)));
                img.BackgroundColor = System.Drawing.Color.Transparent;
                if (System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".tiff"
                || System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".tif")
                {
                    img.RemoveProfile("tiff:37724");
                }
                img.Extent(Convert.ToInt32(Math.Floor(MaxWidth)), Convert.ToInt32(Math.Floor(MaxHeight)), Gravity.Center, new MagickColor(System.Drawing.Color.White));

                imageFile.imagename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(imageFile.imagename);
                img.Write(Folder + imageFile.imagename);
            }
            #endregion

Here is my crop code. I am using a javascript cropping tool, and the new image dimensions are passed to this routine. The cropping works, it's the grain of the photo that is unacceptable.
            using (MagickImage img = new MagickImage(folder + thisModel.originalURL))
            {
                if (img.Quality != 100 || img.Depth != 24)
                {
                    img.Quality = 100;
                    img.Depth = 24;
                    img.Resample(new PointD(300, 300));
                }
                img.Crop(new MagickGeometry(Convert.ToInt32(x), Convert.ToInt32(y), Convert.ToInt32(w), Convert.ToInt32(h)));
                #region Now we upsize it
                Double resX = img.ResolutionX;
                Double resY = img.ResolutionY;
                if (resX == 0 || resY == 0)
                {
                    resX = img.Density.X;
                    resY = img.Density.Y;
                }
                img.Resize(Convert.ToInt32(Math.Floor(widthNeeded * Convert.ToSingle(resX))), Convert.ToInt32(Math.Floor(heightNeeded * Convert.ToSingle(resY))));
                #endregion
                img.Write(folder + thisModel.cropURL);
            }
Aug 19, 2016 at 7:33 PM
The trick with PDF files is to specify the DPI when you read the file.
MagickReadSettings settings = new MagickReadSettings()
{
  Density = new Density(96)
};

using (MagickImage image = new MagickImage("your.pdf", settings))
{
}
Can you test if that helps to resolve your issue?
Aug 19, 2016 at 7:48 PM
That may help with PDF, but we are seeing this with all files. The attached file, for example, is the one I am testing with.
Aug 19, 2016 at 8:01 PM
Edited Aug 19, 2016 at 8:01 PM
Attaching did not work. Sharing it on dropbox/onedrive works better here. Why are you resampling the image? Wouldn't it be better to just specify the Density?
Aug 19, 2016 at 8:05 PM
Aug 19, 2016 at 8:22 PM
Edited Aug 19, 2016 at 8:22 PM
That image already has the correct size. You would only need to resize larger images to 750x413, set the Density property of the image to 300 and save it as a format that supports DPI.
Aug 19, 2016 at 9:19 PM
While that may be true, what part of the process is causing it to get so grainy? Somewhere along the way it loses resolution?
Aug 20, 2016 at 10:29 AM
It most likely gets grainy because your input image is smaller than your target size. Was that gif image the original input image?
Sep 1, 2016 at 6:43 PM

Sorry, I have been out of town. Yes, the gif that I upload was the first image. If you run that through the code I put in the ticket, you will see that it gets extremely grainy. I just need to figure out why it gets so grainy so I can correct the problem.

Thanks