Is Degradation in Quality Necessary?

  • Thread starter Thread starter Cecil Windham
  • Start date Start date
C

Cecil Windham

I'm using the following code to rotate an image (jpeg or gif), and have
discovered that there is a significant degradation in the quality of the
photo.

Just wondering if there is a way to rotate/flip a graphic in .NET *without*
data loss. If so, how? If not, why?

I understand that when *resizing* a graphic, some degradation in the
appearance will result from the loss of data. However, when it comes to
simply rotating or flipping a graphic, is it necessary that any data loss
will occur?

System.IO.FileStream fs = new System.IO.FileStream(pathToOriginal,
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.Drawing.Image imageToFlip = System.Drawing.Image.FromStream(fs);
imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
// then either of these depending on the origina'ls format (jpeg or gif):
imageToFlip.Save(pathToOriginal, System.Drawing.Imaging.ImageFormat.Jpeg);
imageToFlip.Save(pathToOriginal, System.Drawing.Imaging.ImageFormat.Gif);

Thanks.
 
Cecil said:
I'm using the following code to rotate an image (jpeg or gif), and have
discovered that there is a significant degradation in the quality of the
photo.

Just wondering if there is a way to rotate/flip a graphic in .NET *without*
data loss. If so, how? If not, why?

I understand that when *resizing* a graphic, some degradation in the
appearance will result from the loss of data. However, when it comes to
simply rotating or flipping a graphic, is it necessary that any data loss
will occur?

System.IO.FileStream fs = new System.IO.FileStream(pathToOriginal,
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.Drawing.Image imageToFlip = System.Drawing.Image.FromStream(fs);
imageToFlip.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
// then either of these depending on the origina'ls format (jpeg or gif):
imageToFlip.Save(pathToOriginal, System.Drawing.Imaging.ImageFormat.Jpeg);
imageToFlip.Save(pathToOriginal, System.Drawing.Imaging.ImageFormat.Gif);

Thanks.

As you are aware, jpeg compression is lossy. During a rotate, the image has to
be decompressed, rotated, and then recompressed. The recompression will cause
inevitable loss of quality.

I don't know the details of the compressor, but most jpeg compressors have a
quality (and other) settings to be used during compression. The default may be
to minimize size (at the expense of visual quality). Check the docs and see if
you can change to a higher quality compression.
 
Just to add to this response, there is a way to do a lossless rotation of a
JPEG image. A search on Google should turn up some results since it is
widely used in digital image catalogue programs.

Colin
 
Back
Top