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

Still can't get resize/resample to work with any consistency

Oct 6, 2016 at 9:34 PM
Edited Oct 6, 2016 at 9:36 PM
My customer has this rule:
No matter what size or DPI image a user uploads, make it 300 DPI. The resulting dimensions must be 2.13 x 2.5. I must keep the aspect ratio. I fill in 'white' color where I need to pad the image. The user can upload any format, including PDF or TIFF.

But no matter what I do, the resulting image is always saved at the original DPI, or otherwise screwed beyond belief.. The DPI does not consistently change, so then when I resize it, it always is the wrong size. I've tried literally 1000s of ways to do this, and cannot find anything that works. I have been working on this for almost a year, and have never got it to work consistently. This is at least my third or fourth post here, but no answers so far have solved the issue.

Here is my current code, which does not work, and this is iteration at least 1000.
            Single MaxWidth = 0;
            Single MaxHeight = 0;
            Single shrinkRatio = 0;
            using (MagickImage getRatio = new MagickImage(Folder + imageFile.imagename))
            {
                imageFile.HR = Convert.ToSingle(getRatio.ResolutionX);
                if (imageFile.HR <= 1)
                    imageFile.HR = Convert.ToSingle(getRatio.Density.X);
                imageFile.VR = Convert.ToSingle(getRatio.ResolutionY);
                if (imageFile.VR <= 1)
                    imageFile.VR = Convert.ToSingle(getRatio.Density.Y);
                MaxWidth = ImageWidth * imageFile.HR;
                MaxHeight = ImageHeight * imageFile.VR;

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

                if (width != MaxWidth)
                {
                    Single ratio = width / MaxWidth;
                    height = height * ratio;
                    width = MaxWidth;
                    shrinkRatio = Convert.ToSingle(getRatio.Width) / MaxWidth;
                }
                if (height > MaxHeight)
                {
                    Single ratio = height / MaxHeight;
                    width = width * ratio;
                    height = MaxHeight;
                    shrinkRatio = Convert.ToSingle(getRatio.Height) / MaxHeight;
                }
            }
            using (MagickImage resize = new MagickImage(Folder + imageFile.imagename))
            {
                Single density = Convert.ToSingle(resize.Density.X);
                if (shrinkRatio != 0)
                {
                    Single res = density * shrinkRatio;
                    if (res > 300)  // If density is too high, shrink it
                    {
                        Single newRatio = res / 300F;
                        shrinkRatio = shrinkRatio / newRatio;
                        res = 300;
                    }
                    else if (res < 300) // if density is too log, raise it
                    {
                        Single newRatio = 300F/ res;
                        shrinkRatio = shrinkRatio * newRatio;
                        res = 300;
                    }
                    density = res;
                    resize.Resample(res, res);
                    resize.Scale(shrinkRatio * 100D);
                }
                else
                {
                    resize.Resize(Convert.ToInt32(Math.Floor(MaxWidth)), Convert.ToInt32(Math.Floor(MaxHeight)));
                }

                resize.BackgroundColor = System.Drawing.Color.Transparent;
                if (resize.ColorSpace != ColorSpace.sRGB && resize.ColorSpace != ColorSpace.RGB)
                {
                    resize.AddProfile(ColorProfile.SRGB);
                    resize.ColorSpace = ColorSpace.sRGB;
                }
                if (System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".tiff"
                || System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".tif")
                {
                    resize.RemoveProfile("tiff:37724");
                }
                resize.Extent(Convert.ToInt32(Math.Floor(ImageWidth * density)), Convert.ToInt32(Math.Floor(ImageHeight * density)), Gravity.Center, new MagickColor(System.Drawing.Color.White));

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


Oct 6, 2016 at 10:43 PM
Do I understand you correctly that you want your image to have 300 DPI and always be 2.13inch x 2.5inch?
Oct 6, 2016 at 11:05 PM
Correct. Seems simple enough. But I cannot make that work. Also, the image must maintain aspect ratio, and fill in white. So for example, if the resulting image is 2 x 2, we must center it within a white 2.13 x 2.5 white frame. I've tried everything, and
the resulting image sometimes has the correct DPI, sometimes not. I can't find a consistency to why it sometimes works, and sometimes doesn't.
Oct 6, 2016 at 11:21 PM
Two samples to test with attached.