コード例 #1
0
        private Tuple <MediaObjectRotation, Size> RotateUsingGdi(string filePath, int jpegQuality)
        {
            var actualRotation = GalleryObject.CalculateNeededRotation();

            if (actualRotation <= MediaObjectRotation.Rotate0)
            {
                return(new Tuple <MediaObjectRotation, Size>(actualRotation, Size.Empty));
            }

            // Get reference to the bitmap from which the optimized image will be generated.
            using (var originalBitmap = new System.Drawing.Bitmap(filePath))
            {
                var imgFormat = originalBitmap.RawFormat;                 // Need to grab the format before we rotate or else we lose it (it changes to MemoryBmp)

                try
                {
                    originalBitmap.RotateFlip(GetRotateFlipType());
                }
                catch (System.Runtime.InteropServices.ExternalException)
                {
                    throw new UnsupportedImageTypeException();
                }

                ImageHelper.SaveImageToDisk(originalBitmap, GalleryObject.Original.FileNamePhysicalPath, imgFormat, jpegQuality);

                return(new Tuple <MediaObjectRotation, Size>(actualRotation, new Size(originalBitmap.Width, originalBitmap.Height)));
            }
        }
コード例 #2
0
        private Tuple <MediaAssetRotateFlip, Size> RotateFlipUsingWpf(string filePath, int jpegQuality)
        {
            var actualRotation = GalleryObject.CalculateNeededRotation();

            if (actualRotation <= MediaAssetRotateFlip.Rotate0FlipNone)
            {
                return(new Tuple <MediaAssetRotateFlip, Size>(actualRotation, Size.Empty));
            }

            // Grab a reference to the file's metadata properties so we can add them back after the rotation.
            System.Drawing.Imaging.PropertyItem[] propItems = null;
            if (Parent.DisplayType == DisplayObjectType.Original)
            {
                try
                {
                    using (var bmp = new System.Drawing.Bitmap(filePath))
                    {
                        propItems = bmp.PropertyItems;
                    }
                }
                catch (ArgumentException)
                {
                    throw new UnsupportedImageTypeException();
                }
            }

            Tuple <MediaAssetRotateFlip, Size> rotateResult;

            using (var stream = new MemoryStream(File.ReadAllBytes(filePath)))
            {
                var image = GetRotateFlipBitmap(stream, actualRotation);

                var rotatedFlippedImg = BitmapFrame.Create(image);

                var rotatedFlippedBytes = GenerateJpegByteArray(rotatedFlippedImg, jpegQuality);

                File.WriteAllBytes(filePath, rotatedFlippedBytes);

                rotateResult = new Tuple <MediaAssetRotateFlip, Size>(actualRotation, new Size(rotatedFlippedImg.PixelWidth, rotatedFlippedImg.PixelHeight));
            }


            if (rotateResult.Item1 > MediaAssetRotateFlip.Rotate0FlipNone)
            {
                AddMetaValuesBackToRotatedImage(filePath, propItems); // Add meta values back to file
            }

            return(rotateResult);
        }
コード例 #3
0
        private Tuple <MediaAssetRotateFlip, Size> RotateFlipUsingGdi(string filePath, int jpegQuality)
        {
            var actualRotation = GalleryObject.CalculateNeededRotation();

            if (actualRotation <= MediaAssetRotateFlip.Rotate0FlipNone)
            {
                return(new Tuple <MediaAssetRotateFlip, Size>(actualRotation, Size.Empty));
            }

            string tmpImagePath;
            Tuple <MediaAssetRotateFlip, Size> rotateInfo;

            // Get reference to the bitmap from which the optimized image will be generated.
            using (var originalBitmap = new System.Drawing.Bitmap(filePath))
            {
                var imgFormat = originalBitmap.RawFormat; // Need to grab the format before we rotate or else we lose it (it changes to MemoryBmp)

                try
                {
                    originalBitmap.RotateFlip(GetRotateFlipType(actualRotation));
                }
                catch (System.Runtime.InteropServices.ExternalException)
                {
                    throw new UnsupportedImageTypeException();
                }

                // If present, remove the orientation meta property.
                if (Array.IndexOf(originalBitmap.PropertyIdList, ((int)RawMetadataItemName.Orientation)) >= 0)
                {
                    originalBitmap.RemovePropertyItem((int)RawMetadataItemName.Orientation);
                }

                // Save image to temporary location. We can't overwrite the original path because the Bitmap has a lock on it.
                tmpImagePath = Path.Combine(AppSetting.Instance.TempUploadDirectory, String.Concat(Guid.NewGuid().ToString(), ".jpg"));
                ImageHelper.SaveImageToDisk(originalBitmap, tmpImagePath, imgFormat, jpegQuality);

                rotateInfo = new Tuple <MediaAssetRotateFlip, Size>(actualRotation, new Size(originalBitmap.Width, originalBitmap.Height));
            }


            // Now that the original file is freed up, delete it and move the temp file into its place.
            HelperFunctions.MoveFileSafely(tmpImagePath, filePath);

            return(rotateInfo);
        }
コード例 #4
0
        private Tuple<MediaAssetRotateFlip, ISize> RotateFlipImage(string filePath, int jpegQuality)
        {
            var actualRotation = GalleryObject.CalculateNeededRotation();

            if (actualRotation <= MediaAssetRotateFlip.Rotate0FlipNone)
            {
                return new Tuple<MediaAssetRotateFlip, ISize>(actualRotation, Size.Empty);
            }

            string tmpImagePath;
            Tuple<MediaAssetRotateFlip, ISize> rotateInfo;

            using (var image = SixLabors.ImageSharp.Image.Load(filePath))
            {
                image.Mutate(x => x.RotateFlip(GetRotateType(actualRotation), GetFlipType(actualRotation)));

                // Save image to temporary location, then replace the original.
                var fileExtension = Path.GetExtension(filePath);
                tmpImagePath = Path.Combine(AppSetting.Instance.TempUploadDirectory, string.Concat(Guid.NewGuid().ToString(), fileExtension));

                //TODO: Make sure orientation metadata is removed and others are preserved and/or updated (e.g. width/height need updates)
                if (IsJpeg(fileExtension))
                {
                    image.Save(tmpImagePath, new JpegEncoder() { IgnoreMetadata = false, Quality = jpegQuality });
                }
                else
                {
                    image.Save(tmpImagePath); // Encoder is inferred from file extension
                }

                rotateInfo = new Tuple<MediaAssetRotateFlip, ISize>(actualRotation, new Size(image.Width, image.Height));
            }

            // Now that the original file is freed up, delete it and move the temp file into its place.
            if (File.Exists(filePath))
                File.Delete(filePath);

            File.Move(tmpImagePath, filePath);

            return rotateInfo;
        }
コード例 #5
0
        private Tuple <MediaObjectRotation, Size> RotateUsingWpf(string filePath, int jpegQuality)
        {
            var actualRotation = GalleryObject.CalculateNeededRotation();

            if (actualRotation <= MediaObjectRotation.Rotate0)
            {
                return(new Tuple <MediaObjectRotation, Size>(actualRotation, Size.Empty));
            }

            var bytes = File.ReadAllBytes(filePath);

            using (var stream = new MemoryStream(bytes))
            {
                var image = new TransformedBitmap(ReadBitmapFrame(stream), new RotateTransform(GetRotationInDegrees(actualRotation)));

                var rotatedImg = BitmapFrame.Create(image);

                var rotatedBytes = GenerateJpegByteArray(rotatedImg, jpegQuality);

                File.WriteAllBytes(filePath, rotatedBytes);

                return(new Tuple <MediaObjectRotation, Size>(actualRotation, new Size(rotatedImg.PixelWidth, rotatedImg.PixelHeight)));
            }
        }