示例#1
0
 protected void OnBeforeWritingFile(ResizeResult result)
 {
     if (BeforeWritingFile != null)
     {
         BeforeWritingFile(this, new ResizerEventArgs(result));
     }
 }
示例#2
0
        private void Rotate(string sourcePath, bool doRotateRight)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(sourcePath));
            Contract.Ensures(!String.IsNullOrWhiteSpace(Contract.Result <string>()));

            Image img = Image.FromFile(sourcePath);

            if (doRotateRight)
            {
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
            else
            {
                img.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }

            string destinationPath = sourcePath;

            var fileExists = File.Exists(destinationPath);
            var finalPath  = destinationPath;
            var result     = new ResizeResult(finalPath, FileUtil.CreateActionMessage(finalPath, _processingCmd));

            result.SourceFileName = sourcePath;
            result.SizeBefore     = new FileInfo(sourcePath).Length;


            if (fileExists)
            {
                destinationPath = Path.GetTempFileName();
            }

            img.Save(destinationPath);

            // Move any existing file to the Recycle Bin
            if (fileExists)
            {
                OnBeforeWritingFile(result);
                // TODO: Is there a better way to do this without a reference to Microsoft.VisualBasic?
                //FileSystem.DeleteFile(finalPath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                File.Delete(finalPath);
                File.Move(destinationPath, finalPath);
                //if (_processingCmd.AddToSolution)
                //   Ima
            }

            result.SizeAfter    = new FileInfo(finalPath).Length;
            result.PercentSaved = result.SizeBefore > 0 ? Math.Round((result.SizeBefore - result.SizeAfter) / result.SizeBefore * 100, 2) : 0;

            OnProgress(new ResizerEventArgs(result));
        }
示例#3
0
        private void ConvertToSpecificType(string sourcePath)
        {
            string destinationPath = sourcePath;
            Bitmap b;

            switch (_processingCmd.ActionToPerform)
            {
            case ManipulateAction.ConvertToJpeg:
                //Resize(sourcePath, encoder);
                destinationPath = sourcePath.Replace(Path.GetExtension(sourcePath), ".jpg");
                b = new Bitmap(Image.FromFile(sourcePath));
                b.Save(destinationPath, ImageFormat.Jpeg);
                break;

            case ManipulateAction.ConvertToPng:
                destinationPath = sourcePath.Replace(Path.GetExtension(sourcePath), ".png");
                b = new Bitmap(Image.FromFile(sourcePath));
                b.Save(destinationPath, ImageFormat.Png);
                break;

            case ManipulateAction.ConvertToGif:
                destinationPath = sourcePath.Replace(Path.GetExtension(sourcePath), ".gif");
                b = new Bitmap(Image.FromFile(sourcePath));
                b.Save(destinationPath, ImageFormat.Gif);
                break;
            }

            var result = new ResizeResult(destinationPath, FileUtil.CreateActionMessage(destinationPath, _processingCmd));

            result.SourceFileName = sourcePath;
            result.SizeBefore     = new FileInfo(sourcePath).Length;
            result.SizeAfter      = new FileInfo(destinationPath).Length;
            result.PercentSaved   = result.SizeBefore > 0 ? Math.Round((result.SizeBefore - result.SizeAfter) / result.SizeBefore * 100, 2) : 0;

            OnProgress(new ResizerEventArgs(result));
        }
示例#4
0
 public ResizerEventArgs(ResizeResult result)
 {
     this.Result = result;
 }
示例#5
0
        private void Resize(string sourcePath, BitmapEncoder overrideEncoder = null)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(sourcePath));
            Contract.Ensures(!String.IsNullOrWhiteSpace(Contract.Result <string>()));

            //string extension = Path.GetExtension(sourcePath).ToUpperInvariant();
            string destinationPath = sourcePath;

            switch (_processingCmd.ActionToPerform)
            {
            case ManipulateAction.CopyAsThumbnail:
                _processingCmd.CreateNewFile = true;
                _processingCmd.PostFixFile   = "_thumb";
                break;
            }

            if (_processingCmd.CreateNewFile)
            {
                destinationPath = FileUtil.GenerateNewFileName(sourcePath, _processingCmd.PostFixFile);
            }

            BitmapDecoder decoder;
            BitmapEncoder encoder;

            using (var sourceStream = File.OpenRead(sourcePath))
            {
                // NOTE: Using BitmapCacheOption.OnLoad here will read the entire file into
                //       memory which allows us to dispose of the file stream immediately
                decoder = BitmapDecoder.Create(sourceStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
            }

            //See if a destination file type is specified
            if (overrideEncoder == null)
            {
                encoder = BitmapEncoder.Create(decoder.CodecInfo.ContainerFormat);

                try
                {
                    // NOTE: This will throw if the codec dose not support encoding
                    var _ = encoder.CodecInfo;
                }
                catch (NotSupportedException)
                {
                    // Fallback to JPEG encoder
                    encoder = CreateEncoder(DefaultJpegEncoderType);
                }
            }
            else
            {
                encoder = overrideEncoder;
            }

            // TODO: Copy container-level metadata if codec supports it
            SetEncoderSettings(encoder);


            // NOTE: Only TIFF and GIF images support multiple frames
            foreach (var sourceFrame in decoder.Frames)
            {
                // Apply the transform
                var transform         = GetTransform(sourceFrame);
                var transformedBitmap = new TransformedBitmap(sourceFrame, transform);

                // TODO: Optionally copy metadata
                // Create the destination frame
                var thumbnail        = sourceFrame.Thumbnail;
                var metadata         = sourceFrame.Metadata as BitmapMetadata;
                var colorContexts    = sourceFrame.ColorContexts;
                var destinationFrame = BitmapFrame.Create(transformedBitmap, thumbnail, metadata, colorContexts);

                encoder.Frames.Add(destinationFrame);
            }


            var fileExists = File.Exists(destinationPath);
            var finalPath  = destinationPath;
            var result     = new ResizeResult(finalPath, FileUtil.CreateActionMessage(finalPath, _processingCmd));

            result.SourceFileName = sourcePath;
            result.SizeBefore     = new FileInfo(sourcePath).Length;


            if (fileExists)
            {
                destinationPath = Path.GetTempFileName();
            }

            using (var destinationStream = File.OpenWrite(destinationPath))
            {
                // Save the final image
                encoder.Save(destinationStream);
            }



            // Move any existing file to the Recycle Bin
            if (fileExists)
            {
                OnBeforeWritingFile(result);
                // TODO: Is there a better way to do this without a reference to Microsoft.VisualBasic?
                //FileSystem.DeleteFile(finalPath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                File.Delete(finalPath);
                File.Move(destinationPath, finalPath);
            }

            result.SizeAfter    = new FileInfo(finalPath).Length;
            result.PercentSaved = result.SizeBefore > 0 ? Math.Round((result.SizeBefore - result.SizeAfter) / result.SizeBefore * 100, 2) : 0;

            OnProgress(new ResizerEventArgs(result));
        }