Exemple #1
0
        protected override RequestedAction PostDecodeStream(ref Bitmap b, ResizeSettings settings)
        {
            if (!"true".Equals(settings["autorotate"], StringComparison.OrdinalIgnoreCase)) return RequestedAction.None;

            int propertyId = 0x0112;
            PropertyItem pi;
            try {
                pi = b.GetPropertyItem(propertyId);
            } catch (ArgumentException) {
                return RequestedAction.None;
            }
            if (pi == null) return RequestedAction.None;

            int total = 0;

            foreach (byte by in pi.Value) total += by; //Does not handle values larger than 255, but it doesn't need to, and is endian-agnostic.

            if (total == 8) b.RotateFlip(RotateFlipType.Rotate270FlipNone);
            if (total == 3) b.RotateFlip(RotateFlipType.Rotate180FlipNone);
            if (total == 6) b.RotateFlip(RotateFlipType.Rotate90FlipNone);

            if (total == 2) b.RotateFlip(RotateFlipType.RotateNoneFlipX);
            if (total == 4) b.RotateFlip(RotateFlipType.Rotate180FlipX);
            if (total == 5) b.RotateFlip(RotateFlipType.Rotate270FlipY);
            if (total == 7) b.RotateFlip(RotateFlipType.Rotate90FlipY);

            b.RemovePropertyItem(propertyId);

            return RequestedAction.None;
        }
        private static Bitmap CorrectRotation(Bitmap bitmap)
        {
            if (Array.IndexOf (bitmap.PropertyIdList, 274) > -1)
              {
            var values = bitmap.GetPropertyItem (274).Value;
            //var dateTakenPropertyItem = bitmap.GetPropertyItem (0x9003);
            //var dateTakenString = Encoding.UTF8.GetString (dateTakenPropertyItem.Value);
            //var regex = new Regex (":");
            //dateTakenString = dateTakenString.Substring (0, dateTakenString.Length - 1);
            //dateTakenString = regex.Replace (dateTakenString, "-", 2);
            //var dateTaken = DateTime.Parse (dateTakenString);

            if (values.Length < 1)
              return bitmap;

            var orientation = (int) values [0];
            switch (orientation)
            {
              case 1:
            // No rotation required.
            break;
              case 2:
            bitmap.RotateFlip (RotateFlipType.RotateNoneFlipX);
            break;
              case 3:
            bitmap.RotateFlip (RotateFlipType.Rotate180FlipNone);
            break;
              case 4:
            bitmap.RotateFlip (RotateFlipType.Rotate180FlipX);
            break;
              case 5:
            bitmap.RotateFlip (RotateFlipType.Rotate90FlipX);
            break;
              case 6:
            bitmap.RotateFlip (RotateFlipType.Rotate90FlipNone);
            break;
              case 7:
            bitmap.RotateFlip (RotateFlipType.Rotate270FlipX);
            break;
              case 8:
            bitmap.RotateFlip (RotateFlipType.Rotate270FlipNone);
            break;
            }
            // This EXIF data is now invalid and should be removed.
            bitmap.RemovePropertyItem (274);
              }
              return bitmap;
        }
        private EndianStream skipExifData(EndianStream image)
        {
            Bitmap bitmap = new Bitmap(image.BaseStream);
            Stream output = new MemoryStream();
            // load into bitmap, remove all exlif data
            foreach (PropertyItem item in bitmap.PropertyItems)
            {
                if (item.Type == 2)
                    bitmap.RemovePropertyItem(item.Id);
            }

            bitmap.Save(output, ImageFormat.Jpeg);
            return new EndianStream(output, image.Endianness);
        }