Monday, December 1, 2008

To save and rotate the JPEG Image without affecting the quality

Hi,
If we rotate and save the JPEG Image often.The quality get reduced and the image seems to be blur.we can overcome this using the following,

public bool Rotate(RotationType type, string path)
{
try
{
img = Image.FromFile(path);
img.Save(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp", ImageFormat.Bmp);
ImageCodecInfo usedIC = this.GetEncoderInfo("image/bmp");
Encoder encoder = Encoder.Quality;
EncoderParameters encparams = new EncoderParameters(1);
EncoderParameter encparam = new EncoderParameter(encoder, (long)100);
encparams.Param[0] = encparam;
Image test = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp");
img.Dispose();
img = null;
if (type == RotationType.Rotate90)
test.RotateFlip(RotateFlipType.Rotate90FlipNone);
else if (type == RotationType.Rotate180)
test.RotateFlip(RotateFlipType.Rotate180FlipNone);
else if (type == RotationType.Rotate270)
test.RotateFlip(RotateFlipType.Rotate270FlipNone);
test.Save(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp", usedIC, encparams);
test.Save(path, usedIC, encparams);
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp");
GC.Collect();
return true;

}

Here rotationtype->Indicates rotationtype by 90-Degree,180-Degree,270-Degree or 360-Degree

No comments: