Is it possible to find out if a JPEG image was rotated only from its raw bytes?

Can you tell (say, using .NET 4.0, WinForms) if a JPEG image is rotated only from its binary file (for example, the result of File.ReadAllBytes() )?

UPDATE


Thank you all for your answers.

Just a heads-up for anyone trying to solve the same problem. I was tricked by the System.Drawing.Image class, which loads EXIF ​​tags when initializing with FromFile(...) , but seems to ignore them when initializing from a stream. I used the ExifTagCollection library to read EXIF ​​tags, but I think the results will be comparable to any other library.

 var bytes = (get binary from server) File.WriteAllBytes(path, bytes); 

WORKS :

 var image = Image.FromFile(path); 

DOES NOT WORK : (not executed for FileStream )

 using (var ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); } 

Continuation:

 ExifTagCollection exif = new ExifTagCollection(image); foreach (ExifTag tag in exif) { Console.WriteLine(tag.ToString()); } 

no tags when loading from stream.

+4
source share
4 answers

http://jpegclub.org/exif_orientation.html details the exif orientation flag. Find it, find the orientation.

Of course, this only applies to image rotation, setting this flag, as the cameras themselves often do, some kind of image viewing software that is not intended for more detailed editing, and some straightforward -manager files. This will not work if someone uploaded the image to a more general image editor, expanded it and saved it.

Edit:

Landscape vs. Portrait is different from "rotated with the natural orientation of graphic devices." It is also simpler:

 if(img.Height == img.Width) return MyAspectEnum.Square; if(img.Height > img.Width) return MyAspectEnum.Portrait; return MyAspectEnum.Landscape; 

It may be closer to what you really want to know.

+4
source

If EXIF ​​data is not available / not reliable, you can assume this to determine the image format:

  • Height> Width? Portrait format
  • Width> Height? Landscape format
  • Width = Height? The image is perfectly square, or one is beautiful.

The same restriction as in EXIF: physical editing, which wrapped around the image and did not update / display EXIF ​​information, would also deceive this check.

+2
source

If you know how to read JPEG data, you can search for EXIF ​​and get the rotation from EXIF. It would be difficult if EXIF ​​data were not available.

+1
source

You need to read EXIF ​​to determine the orientation of a JPEG image.

Please see ExifLib - Fast Exif Data Extractor for .NET 2.0+ . The library seems to return the orientation as indicated here .

+1
source

All Articles