因此,我正在从目录中读取文件,确定它们需要旋转的方式。旋转然后保存。这部分工作...问题是,它保存文件后,它重新压缩,我从1.5meg图像到250k图像。我需要保持原来的文件大小。我尝试使用jhead.exe并从命令行调用它,但无法让我的任何参数正确传入。这是我的代码片段来检测,旋转和保存。C#在不损失太多质量的情况下旋转JPG
foreach (FileInfo f in dir.GetFiles("*.jpg"))
{
try
{
string ExportName = "";
Bitmap originalImage = new Bitmap(f.FullName.ToString());
Info inf = new Info(originalImage);
gma.Drawing.ImageInfo.Orientation orientation = gma.Drawing.ImageInfo.Orientation.TopLeft;
try
{
orientation = inf.Orientation;
}
catch
{
orientation = gma.Drawing.ImageInfo.Orientation.TopLeft;
}
originalImage = CheckRotation(originalImage, orientation);
progressBar.Value = progressBar.Value + 1;
originalImage.Save(f.FullName.ToString(), ImageFormat.Jpeg);
Application.DoEvents();
}
private Bitmap CheckRotation(Bitmap inputImage, gma.Drawing.ImageInfo.Orientation orientation)
{
Bitmap rotatedImage = inputImage;
switch (orientation)
{
case gma.Drawing.ImageInfo.Orientation.LeftBottom:
rotatedImage.RotateFlip(RotateFlipType.Rotate90FlipXY);
break;
case gma.Drawing.ImageInfo.Orientation.RightTop:
rotatedImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
default:
break;
}
return rotatedImage;
}
我会建议你使用一个开源工具进行无损旋转(如jhead.exe出现)。你为什么不用你用来运行jhead的代码打开另一个问题? – Douglas 2012-02-29 19:51:11
您需要使用一个'Save'重载,它需要一个'ImageCodecInfo'和'EncoderParameters' - 参数应该包含高质量,以确保图像不会显着降级。 – Oded 2012-02-29 19:51:54
@Oded:你的意思是“不会显着降低”。将图像编码为JPEG时,即使以“100%”质量进行编码,实际上也总会出现退化。根据定义,格式是有损的。 – Douglas 2012-02-29 19:53:58