简体   繁体   中英

How to make sure we get a lossless image

I'm trying to write a code that gets an Image (PNG, JPG, BMP, ETC') crops and rotate the image. I want to crop the image without losing information (no change in interpolation) so i am using

        Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
        using (Graphics g = Graphics.FromImage(target))
        {
            g.DrawImage(img, new Rectangle(0, 0, target.Width, target.Height),
                             cropRect,
                             GraphicsUnit.Pixel);
        }

where target is the cropped image. img is the originial image and cropRect is the cropping rectangle i want to crop. Because i maintain the image size (no zoom) there shouldn't be any information lost.

Afterwards i am rotating the image using

        target.RotateFlip(RotateFlipType.Rotate90FlipNone);

because it is 90 degrees rotation - it should remain lossless. Is that correct? I couldn't find any documention on that subject, if anybody has a link i would appreciate it!

You could use DrawImageUnscaled / DrawImageUnscaledAndClipped to absolutely make sure it doesn't do anything funky, although I doubt it would anyway in your case. And since it's 90 degree increments and/or a flip, RotateFlip should not degrade the image.

However, if you resave the image as a non-lossless format, you'll certainly lose some information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM