Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

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

$
0
0
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);
            }



Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>