I am doing a 90° rotation with minimal loss, using the following code:
System.Drawing.Image originalImage = System.Drawing.Image.FromFile("input.jpg");
ImageFormat sourceFormat = originalImage.RawFormat;
EncoderParameters encoderParams = null;
try
{
if (sourceFormat.Guid == ImageFormat.Jpeg.Guid)
{
encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(Encoder.Transformation,
(long)EncoderValue.TransformRotate90);
}
originalImage.Save("output.jpg", GetEncoder(sourceFormat), encoderParams);
}
finally
{
if (encoderParams != null)
encoderParams.Dispose();
}
However the Save function seems to create EXIF metadata from the original (pure, no EXIF) JPEG COM marker (0xFE). I do not want EXIF markers in the output JPEG. I also want to preserve the original COM marked. What C# API in my application can I use instead to save my rotated buffer ?
Using jpegdump (dicom3tools package):
$ jpegdump < input.jpg
[...]
Offset 0x0014 Marker 0xfffe COM Comment length variable 0x10
While:
$ jpegdump < output.jpg
[...]
Offset 0x0014 Marker 0xffe1 APP1 Reserved for Application Use length variable 0x5a
Turns out the only working solution I could come up with, was saving the JPEG to a MemoryStream and then post process this temporary Stream using the solution described at:
Pseudo code:
var jpegPatcher = new JpegPatcher();
FileStream outFile = new FileStream(fileName, FileMode.Open, FileAccess.Write);
jpegPatcher.PatchAwayExif(inStream, outFile);
I used the code from the blog, which does:
private void SkipAppHeaderSection(Stream inStream)
...
while (header[0] == 0xff && (header[1] >= 0xe0 && header[1] <= 0xef))
So the function name PatchAwayExif is a bit odd since it also remove the APP0 (aka JFIF) segment...but that was something I also needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With