B
Brian Smith
As has been documented on several sites, the CLR Imaging library supports
(theoretically) using a lossless rotation algorithm for jpeg files. This is
also used as a trick to re-Save an image without causing re-compression.
This does not seem to work in 1.1 - on most images I try, the file size is
reduced, and the resultant contents are subtly changed. Has this been broken
in the 1.1 framework, or is there some other method?
brian
---------------------------------------
sample code:
public void Rotate90(string fileName)
{
string FileNameTemp;
using (Image Pic = Image.FromFile(fileName))
{
Encoder Enc = Encoder.Transformation;
EncoderParameters EncParms = new EncoderParameters(1);
EncoderParameter EncParm;
ImageCodecInfo CodecInfo = GetEncoderInfo("image/jpeg");
// we cannot store in the same image, so use a temporary image instead
FileNameTemp = Path.GetDirectoryName(fileName) + "\\rotated " +
Path.GetFileName(fileName);
if (File.Exists(FileNameTemp))
File.Delete(FileNameTemp) ;
// for rewriting without recompression we must rotate the image 90
degrees
EncParm = new
EncoderParameter(Enc,(long)EncoderValue.TransformRotate90);
EncParms.Param[0] = EncParm;
// now save the rotated image
Pic.Save(FileNameTemp,CodecInfo,EncParms);
}
// delete the original file
File.Delete(fileName);
// rename to original
File.Move(FileNameTemp, fileName);
}
(theoretically) using a lossless rotation algorithm for jpeg files. This is
also used as a trick to re-Save an image without causing re-compression.
This does not seem to work in 1.1 - on most images I try, the file size is
reduced, and the resultant contents are subtly changed. Has this been broken
in the 1.1 framework, or is there some other method?
brian
---------------------------------------
sample code:
public void Rotate90(string fileName)
{
string FileNameTemp;
using (Image Pic = Image.FromFile(fileName))
{
Encoder Enc = Encoder.Transformation;
EncoderParameters EncParms = new EncoderParameters(1);
EncoderParameter EncParm;
ImageCodecInfo CodecInfo = GetEncoderInfo("image/jpeg");
// we cannot store in the same image, so use a temporary image instead
FileNameTemp = Path.GetDirectoryName(fileName) + "\\rotated " +
Path.GetFileName(fileName);
if (File.Exists(FileNameTemp))
File.Delete(FileNameTemp) ;
// for rewriting without recompression we must rotate the image 90
degrees
EncParm = new
EncoderParameter(Enc,(long)EncoderValue.TransformRotate90);
EncParms.Param[0] = EncParm;
// now save the rotated image
Pic.Save(FileNameTemp,CodecInfo,EncParms);
}
// delete the original file
File.Delete(fileName);
// rename to original
File.Move(FileNameTemp, fileName);
}