Exemplo n.º 1
0
 public void RotateImageIfRequired(Image originalImage)
 {
     if (ImageNeedsRotation(originalImage))
     {
         ExifOrientations imageRotation = DetermineImageRotation(originalImage);
         RotateImageUsingExifOrientation(originalImage, imageRotation);
     }
 }
Exemplo n.º 2
0
        // 适正
        public static void Orientate(Image image)
        {
            try {
                // Get the index of the orientation property.
                int orientation_index = Array.IndexOf(image.PropertyIdList, EXIF_ORIENTATION_ID);
                // If there is no such property, return Unknown.
                if (orientation_index < 0)
                {
                    return;
                }
                PropertyItem item = image.GetPropertyItem(EXIF_ORIENTATION_ID);

                ExifOrientations orientation = (ExifOrientations)item.Value[0];
                // Orient the image.
                switch (orientation)
                {
                case ExifOrientations.Unknown:
                case ExifOrientations.TopLeft:
                    break;

                case ExifOrientations.TopRight:
                    image.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    break;

                case ExifOrientations.BottomRight:
                    image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;

                case ExifOrientations.BottomLeft:
                    image.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    break;

                case ExifOrientations.LeftTop:
                    image.RotateFlip(RotateFlipType.Rotate90FlipX);
                    break;

                case ExifOrientations.RightTop:
                    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;

                case ExifOrientations.RightBottom:
                    image.RotateFlip(RotateFlipType.Rotate90FlipY);
                    break;

                case ExifOrientations.LeftBottom:
                    image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;
                }
                // Set the orientation to be normal, as we rotated the image.
                item.Value[0] = (byte)ExifOrientations.TopLeft;
                image.SetPropertyItem(item);
            }
            catch (Exception orientEx) {
                log.Error("Failed to Orientate, " + orientEx.Message);
            }
        }
Exemplo n.º 3
0
        public static Image GetImage(byte[] _data)
        {
            if (_data != null)
            {
                using (MemoryStream ms = new MemoryStream(_data))
                {
                    Image image = Image.FromStream(ms);

                    foreach (var prop in image.PropertyItems)
                    {
                        if (prop.Id == 0x112 || prop.Id == 0x5029)
                        {
                            byte[] propValue = prop.Value;

                            if (propValue.Length > 0)
                            {
                                ExifOrientations orientation = (ExifOrientations)propValue[0];

                                switch (orientation)
                                {
                                case ExifOrientations.TopLeft:
                                    //Do nothing;
                                    break;

                                case ExifOrientations.BottomRight:
                                    //Rotate 180

                                    image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                    break;

                                case ExifOrientations.RightTop:
                                    //Rotate clock wise 90

                                    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                    break;

                                case ExifOrientations.LeftBottom:
                                    //Rotate ccw 90

                                    image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                    break;
                                }
                            }
                        }
                    }

                    return(image);
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        private static void SetImageOrientation(Image img, ExifOrientations orientation)
        {
            int orientationIndex = Array.IndexOf(img.PropertyIdList, OrientationId);

            if (orientationIndex < 0)
            {
                return;
            }

            PropertyItem item = img.GetPropertyItem(OrientationId);

            item.Value[0] = (byte)orientation;
            img.SetPropertyItem(item);
        }
        public static ThumbnailInfo MakeThumbnail(Bitmap origin, ImageMeta meta, ExifOrientations orientation,
                                                  string attachmentId, Driver driver, string origFileName)
        {
            Bitmap thumbnail = null;

            if (meta == ImageMeta.Square)
            {
                thumbnail = MakeSquareThumbnail(origin);
            }
            else
            {
                thumbnail = ImageHelper.ScaleBasedOnLongSide(origin, (int)meta);
            }

            try
            {
                if (orientation != ExifOrientations.Unknown)
                {
                    ImageHelper.CorrectOrientation(orientation, thumbnail);
                }
                else
                {
                    ImageHelper.CorrectOrientation(ImageHelper.ImageOrientation(origin), thumbnail);
                }
            }
            catch (System.Runtime.InteropServices.ExternalException ex)
            {
                logger.ErrorFormat("Unable to correct orientation of image {0}", origFileName);
                logger.Error("External exception", ex);
            }

            ImageSaveStrategy imageStrategy  = GetImageSaveStrategy(Path.GetExtension(origFileName));
            SavedResult       savedThumbnail = imageStrategy.Save(thumbnail, attachmentId, meta, driver);

            return(new ThumbnailInfo
            {
                saved_file_name = savedThumbnail.FileName,
                file_name = origFileName,
                width = thumbnail.Width,
                height = thumbnail.Height,
                file_size = savedThumbnail.SavedRawData.Length,
                mime_type = savedThumbnail.MimeType,
                modify_time = DateTime.UtcNow,
                url = "/v2/attachments/view/?object_id=" + attachmentId +
                      "&image_meta=" + meta.ToString().ToLower(),
                RawData = savedThumbnail.SavedRawData
            });
        }
Exemplo n.º 6
0
        public static void CorrectOrientation(ExifOrientations orientation, Image pic)
        {
            switch (orientation)
            {
            case ExifOrientations.TopRight:
                pic.RotateFlip(RotateFlipType.RotateNoneFlipX);
                break;

            case ExifOrientations.BottomRight:
                pic.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;

            case ExifOrientations.BottomLeft:
                pic.RotateFlip(RotateFlipType.RotateNoneFlipY);
                break;

            case ExifOrientations.LeftTop:
                pic.RotateFlip(RotateFlipType.Rotate90FlipX);
                break;

            case ExifOrientations.RightTop:
                pic.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;

            case ExifOrientations.RightBottom:
                pic.RotateFlip(RotateFlipType.Rotate270FlipX);
                break;

            case ExifOrientations.LeftBottom:
                pic.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;

            default:
                return;
            }

            try
            {
                pic.RemovePropertyItem(OrientationId);
            }
            catch
            {
            }
        }
Exemplo n.º 7
0
        private static void RotateImageUsingExifOrientation(Image img, ExifOrientations orientation)
        {
            switch (orientation)
            {
            case ExifOrientations.Unknown:
            case ExifOrientations.TopLeft:
                break;

            case ExifOrientations.TopRight:
                img.RotateFlip(RotateFlipType.RotateNoneFlipX);
                break;

            case ExifOrientations.BottomRight:
                img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;

            case ExifOrientations.BottomLeft:
                img.RotateFlip(RotateFlipType.RotateNoneFlipY);
                break;

            case ExifOrientations.LeftTop:
                img.RotateFlip(RotateFlipType.Rotate90FlipX);
                break;

            case ExifOrientations.RightTop:
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;

            case ExifOrientations.RightBottom:
                img.RotateFlip(RotateFlipType.Rotate90FlipY);
                break;

            case ExifOrientations.LeftBottom:
                img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;
            }

            SetImageOrientation(img, ExifOrientations.TopLeft);
        }
Exemplo n.º 8
0
        public static Bitmap RotateImageBasedOnExif(Image img)
        {
            Bitmap           bmpTarget = new Bitmap(img);
            ExifOrientations exifo     = ExifHelper.ImageOrientation(img);

            switch (exifo)
            {
            case ExifOrientations.RightTop:
                bmpTarget.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;

            case ExifOrientations.BottomRight:
                bmpTarget.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;

            case ExifOrientations.LeftBottom:
                bmpTarget.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;

            default:
                break;
            }
            return(bmpTarget);
        }
Exemplo n.º 9
0
        private static bool ImageNeedsRotation(Image img)
        {
            ExifOrientations imageRotation = DetermineImageRotation(img);

            return(imageRotation != ExifOrientations.Unknown && imageRotation != ExifOrientations.TopLeft);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Make an image to demonstrate orientations.
        /// </summary>
        /// <param name="orientation"></param>
        /// <returns></returns>
        public static Image OrientationImage(ExifOrientations orientation)
        {
            const int size = 64;
            Bitmap    bm   = new Bitmap(64, 64);

            using (Graphics gr = Graphics.FromImage(bm))
            {
                gr.Clear(Color.White);
                gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

                // Orient the result.
                switch ((orientation))
                {
                case ExifOrientations.TopLeft:
                    break;

                case ExifOrientations.TopRight:
                    gr.ScaleTransform(-1, 1);
                    break;

                case ExifOrientations.BottomRight:
                    gr.RotateTransform(180);
                    break;

                case ExifOrientations.BottomLeft:
                    gr.ScaleTransform(1, -1);
                    break;

                case ExifOrientations.LeftTop:
                    gr.RotateTransform(90);
                    gr.ScaleTransform(-1, 1, MatrixOrder.Append);
                    break;

                case ExifOrientations.RightTop:
                    gr.RotateTransform(-90);
                    break;

                case ExifOrientations.RightBottom:
                    gr.RotateTransform(90);
                    gr.ScaleTransform(1, -1, MatrixOrder.Append);
                    break;

                case ExifOrientations.LeftBottom:
                    gr.RotateTransform(90);
                    break;
                }

                // Translate the result to the center of the bitmap.
                gr.TranslateTransform(size / 2, size / 2, MatrixOrder.Append);
                using (StringFormat string_format = new StringFormat())
                {
                    string_format.LineAlignment = StringAlignment.Center;
                    string_format.Alignment     = StringAlignment.Center;
                    using (Font the_font = new Font("Times New Roman", 40, GraphicsUnit.Point))
                    {
                        if ((orientation == ExifOrientations.Unknown))
                        {
                            gr.DrawString("?", the_font, Brushes.Black, 0, 0, string_format);
                        }
                        else
                        {
                            gr.DrawString("F", the_font, Brushes.Black, 0, 0, string_format);
                        }
                    }
                }
            }

            return(bm);
        }
Exemplo n.º 11
0
        public static string ResizeImage(string orgImageFilePath, string fileName, string resizeRatio, int ratio)
        {
            string _rotatedImagePath = Main.GCONST.TempPath + DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + fileName;

            if (resizeRatio.Equals(string.Empty) || resizeRatio.Equals("100%"))
            {
                return(orgImageFilePath);
            }

            using (Bitmap _img = (Bitmap)Image.FromFile(orgImageFilePath))
            {
                ExifOrientations _exifOrientations = ImageOrientation(_img);

                if (_exifOrientations != ExifOrientations.Unknown)
                {
                    CorrectOrientation(_exifOrientations, _img);
                    _img.Save(_rotatedImagePath);
                }
            }

            int _longestSide = int.Parse(resizeRatio);

            try
            {
                Bitmap _bmp = ResizeImage_Impl(_rotatedImagePath, _longestSide);

                if (_bmp == null)
                {
                    return(_rotatedImagePath);
                }

                if ((_bmp.Height < _longestSide) && (_bmp.Width < _longestSide))
                {
                    return(_rotatedImagePath);
                }

                string _newPath = Main.GCONST.TempPath + DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" +
                                  fileName;

                // Create parameters
                EncoderParameters _params = new EncoderParameters(1);

                // Set quality (100)
                _params.Param[0] = new EncoderParameter(Encoder.Quality, ratio);

                // Create encoder info
                ImageCodecInfo _codec = null;

                foreach (ImageCodecInfo _codectemp in ImageCodecInfo.GetImageDecoders())
                {
                    if (_codectemp.MimeType == FileUtility.GetMimeType(new FileInfo(fileName)))
                    {
                        _codec = _codectemp;
                        break;
                    }
                }

                // Check
                if (_codec == null)
                {
                    throw new Exception("Codec not found!");
                }

                _bmp.Save(_newPath, _codec, _params);

                return(_newPath);
            }
            catch (Exception _e)
            {
                MessageBox.Show(_e.Message);
            }

            return(_rotatedImagePath);
        }
Exemplo n.º 12
0
    private static BitmapSource MetaOrientation(BitmapMetadata meta, BitmapSource ret)
    {
      double angle = 0;
      if ((meta != null) && (ret != null)) //si on a des meta, tentative de récupération de l'orientation
      {
        if (meta.GetQuery("/app1/ifd/{ushort=274}") != null)
        {
          orientation =
            (ExifOrientations)
            Enum.Parse(typeof (ExifOrientations), meta.GetQuery("/app1/ifd/{ushort=274}").ToString());
        }

        switch (orientation)
        {
          case ExifOrientations.Rotate90:
            angle = -90;
            break;
          case ExifOrientations.Rotate180:
            angle = 180;
            break;
          case ExifOrientations.Rotate270:
            angle = 90;
            break;
        }

        if (angle != 0) //on doit effectuer une rotation de l'image
        {
          ret = new TransformedBitmap(ret.Clone(), new RotateTransform(angle));
          ret.Freeze();
        }
      }
      return ret;
    }
Exemplo n.º 13
0
        /// <summary>
        /// Make an image to demonstrate orientations.
        /// </summary>
        /// <param name="orientation"></param>
        /// <returns></returns>
        public static Image OrientationImage(ExifOrientations orientation)
        {
            const int size = 64;
            Bitmap bm = new Bitmap(64, 64);
            using (Graphics gr = Graphics.FromImage(bm))
            {
                gr.Clear(Color.White);
                gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

                // Orient the result.
                switch ((orientation))
                {
                    case ExifOrientations.TopLeft:
                        break;
                    case ExifOrientations.TopRight:
                        gr.ScaleTransform(-1, 1);
                        break;
                    case ExifOrientations.BottomRight:
                        gr.RotateTransform(180);
                        break;
                    case ExifOrientations.BottomLeft:
                        gr.ScaleTransform(1, -1);
                        break;
                    case ExifOrientations.LeftTop:
                        gr.RotateTransform(90);
                        gr.ScaleTransform(-1, 1, MatrixOrder.Append);
                        break;
                    case ExifOrientations.RightTop:
                        gr.RotateTransform(-90);
                        break;
                    case ExifOrientations.RightBottom:
                        gr.RotateTransform(90);
                        gr.ScaleTransform(1, -1, MatrixOrder.Append);
                        break;
                    case ExifOrientations.LeftBottom:
                        gr.RotateTransform(90);
                        break;
                }

                // Translate the result to the center of the bitmap.
                gr.TranslateTransform(size / 2, size / 2, MatrixOrder.Append);
                using (StringFormat string_format = new StringFormat())
                {
                    string_format.LineAlignment = StringAlignment.Center;
                    string_format.Alignment = StringAlignment.Center;
                    using (Font the_font = new Font("Times New Roman", 40, GraphicsUnit.Point))
                    {
                        if ((orientation == ExifOrientations.Unknown))
                        {
                            gr.DrawString("?", the_font, Brushes.Black, 0, 0, string_format);
                        }
                        else
                        {
                            gr.DrawString("F", the_font, Brushes.Black, 0, 0, string_format);
                        }
                    }
                }
            }

            return bm;
        }