0

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!

Sandman
  • 215
  • 1
  • 10

1 Answers1

0

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.

Chris
  • 5,442
  • 17
  • 30
  • (updated with some more information, I realize my initial answer left a bit out) – Chris Dec 30 '14 at 16:27
  • i'm saving it using ImageFormat.Bmp - so it shouldn't lost information. (if the original is Jpeg format - there is no point to lossless and if it's Png format - saving it as Bmp it shouldn't lose information) – Sandman Dec 31 '14 at 13:57
  • Yeah either bmp or png will preserve all information (though not alpha in bmp, if you have that). I'd pick png over bmp all night long. – Chris Dec 31 '14 at 15:28