示例#1
0
        public static byte[] GetRawImageAutoRotatedAndResized(
            string itemFilePath, int width, int height, bool shouldPad)
        {
            try
            {
                bool requiresRotation = ImageRequiresRotation(itemFilePath);

                using (var imageFactory = new ImageFactory())
                {
                    var resizeLayer = new ResizeLayer(new Size(width, height), shouldPad ? ResizeMode.Pad : ResizeMode.Max);

                    imageFactory.Load(itemFilePath);

                    if (requiresRotation)
                    {
                        imageFactory.AutoRotate();
                    }

                    imageFactory.Resize(resizeLayer);

                    using (var ms = new MemoryStream())
                    {
                        imageFactory.Save(ms);
                        return(ms.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Error(ex, $"Could not auto-rotate and resize image {itemFilePath}");
            }

            return(null);
        }
示例#2
0
        private void ResizeImage(Stream sourceFileStream, string destinationFilePath, Size size, ResizeMode mode, Color backgroundColor, int rotationDegrees)
        {
            ISupportedImageFormat format = GetImageFormat(destinationFilePath, this.Quality);

            using (MemoryStream outStream = new MemoryStream())
            {
                // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                {
                    // Load, resize, set the format and quality and save an image.
                    var image = imageFactory
                                .AutoRotate()
                                .Load(sourceFileStream)
                                .Rotate(rotationDegrees)
                                .Resize(new ResizeLayer(size, resizeMode: mode))
                                .Format(format)
                                .BackgroundColor(backgroundColor)
                                .Save(outStream);
                }

                // Do something with the stream.
                var path = Path.GetDirectoryName(destinationFilePath);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                FileStream file = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write);
                outStream.WriteTo(file);
                file.Close();
            }
        }
示例#3
0
        /// <summary>
        /// 添加水印
        /// </summary>
        /// <param name="imageStream"></param>
        /// <returns></returns>
        private async Task <Stream> AddWaterMarkAsync(Stream imageStream)
        {
            imageStream.Position = 0;
            var stream = new MemoryStream();

            using (var imageF = new ImageFactory())
            {
                imageF.Load(imageStream);
                var image          = imageF.Image;
                var watermarkImage = FileStoreRuleManager.GetWatermarkImage(image.Size);

                var imgl = new ImageLayer();
                imgl.Image = watermarkImage;
                imageF.AutoRotate();
                imageF.Overlay(imgl);

                imageF.Save(stream);
                return(stream);
            }
            using (var image = Image.FromStream(imageStream.Duplicate()))
            {
                var watermarkImage = FileStoreRuleManager.GetWatermarkImage(image.Size);
                return(image.AddWatermark((Image)watermarkImage.Clone(), 5, 100, 10));
            }
        }
示例#4
0
        private void ResizeImage(Stream sourceFileStream, string destinationFilePath, Size size, ResizeMode mode, Color?backgroundColor = null)
        {
            ISupportedImageFormat format = GetImageFormat(destinationFilePath, this.Quality);

            using (MemoryStream outStream = new MemoryStream())
            {
                // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                {
                    // Load, resize, set the format and quality and save an image.
                    var image = imageFactory
                                .AutoRotate()
                                .Load(sourceFileStream);


                    image.Resize(new ResizeLayer(size, resizeMode: mode))
                    .Format(format);

                    //Any padded areas in the output for image formats that do not contain an alpha channel will display as black (the default encoder output). To change this color to another use this option
                    if (backgroundColor != null)
                    {
                        image.BackgroundColor(backgroundColor.Value);
                    }

                    image.Save(outStream);
                }
                // Do something with the stream.
                var path = Path.GetDirectoryName(destinationFilePath);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                FileStream file = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write);
                outStream.WriteTo(file);
                file.Close();
            }
        }
示例#5
0
        /// <summary>
        /// Creates a cropped version of the image at the size specified in the parameters
        /// </summary>
        /// <param name="originalFilePath">The full path of the original file</param>
        /// <param name="newFilePath">The full path of the new file</param>
        /// <param name="width">The new image width</param>
        /// <param name="height">The new image height</param>
        /// <returns>A bool to show if the method was successful or not</returns>
        private bool CreateCroppedImage(string originalFilePath, string newFilePath, int width, int height)
        {
            bool         success      = false;
            ImageFactory imageFactory = new ImageFactory();

            try
            {
                imageFactory.Load(originalFilePath);
                ResizeLayer layer = new ResizeLayer(new System.Drawing.Size(width, height), ResizeMode.Crop, AnchorPosition.Center);
                imageFactory.Resize(layer);
                imageFactory.AutoRotate();
                imageFactory.Save(newFilePath);
                success = true;
            }
            catch (System.Exception)
            {
                success = false;
            }
            finally
            {
                imageFactory.Dispose();
            }
            return(success);
        }