0

I want to crop the image on the fly in c#.

I have referred some links and implement like following.

Reference:

But I am getting the low quality image. I have seen website they are uploading the image in their server and scale down the website without losing quality

How have they done? Why can't we do in c#?

CODE

 public static Image ScaleImage(Image image, int width, int height)
        {
            if (image.Height < height && image.Width < width) return image;
            using (image)
            {
                double xRatio = (double)image.Width / width;
                double yRatio = (double)image.Height / height;
                double ratio = Math.Max(xRatio, yRatio);
                int nnx = (int)Math.Floor(image.Width / xRatio);
                int nny = (int)Math.Floor(image.Height / yRatio);
                Bitmap resizedImage = new Bitmap(nnx, nny, PixelFormat.Format64bppArgb);
                using (Graphics graphics = Graphics.FromImage(resizedImage))
                {
                    graphics.Clear(Color.Transparent);

                    // This is said to give best quality when resizing images
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                    graphics.DrawImage(image,
                       new Rectangle(0, 0, nnx, nny),
                       new Rectangle(0, 0, image.Width, image.Height),
                       GraphicsUnit.Pixel);
                }
                return resizedImage;
            }
        }
Jeeva J
  • 3,173
  • 10
  • 38
  • 85
  • I have had this problem in the past and resolved it but I can't remember how ATM and I'm at work. I somehow want to say you can convert it to JPEG, shrink it, then back to bitmap because I think the scaling of bitmap isn't as good... I'm not sure that's even how I did it but it feels like something I did. It's been years... I'll look into it unless you test and see that, that helps. – Michael Puckett II Feb 22 '17 at 13:27
  • Possible duplicate of [Resize an Image C#](http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp) – Nino Feb 22 '17 at 13:28
  • Cropping should not lose quality. If you scale it down you will always lose information. Do make sure to have the __same dpi__ setting for both images!! - See [here for a similar problem](http://stackoverflow.com/questions/26612057/where-does-this-quality-loss-on-images-come-from/26614735?s=6|0.0626#26614735). Note the second part! - Also [see here about your pixelformat](http://stackoverflow.com/questions/34590509/pixelformat-format32bppargb-vs-pixelformat-format64bppargb) – TaW Feb 22 '17 at 13:46
  • Did you try it? This will be needed for correct results with DrawImage: `Bitmap resizedImage = new Bitmap(nnx, nny, PixelFormat.???); resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);` – TaW Feb 22 '17 at 16:35

3 Answers3

1

You could try net-vips, the C# binding for libvips. It's a lazy, streaming, demand-driven image processing library, so it can do operations like this without needing to load the whole image.

For example, it comes with a handy image thumbnailer:

Image image = Image.Thumbnail("image.jpg", 300, 300);
image.WriteToFile("my-thumbnail.jpg");
kleisauke
  • 515
  • 1
  • 5
  • 7
0

You could use imagemagic for that. It has its own dll to work with VS. It olso have great forum where you can find all kinds of examples. I made a program with it that does that. it took me about half an hour ( not really a big application, it is only used for a commercial company to make a lot of cropped resized pictues (about 4 terrabyte of pictures lol)). here you can find some examples.

https://www.imagemagick.org/script/index.php

0

ImageMagick is a viable solution definitely and so is GraphicsMagick or libvips (Though I am not sure if libvips has C# specific bindings but it is a lot faster than IM or GM ).

You may also want to try ImageKit or other similar fully featured third-party image optimisation and delivery solutions to do all this resizing for you.

jcupitt
  • 10,213
  • 2
  • 23
  • 39
Rahul Nanwani
  • 1,267
  • 1
  • 10
  • 21