Beispiel #1
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            foreach (var file in input.Files)
            {
                Rectangle cropRectangle;
                bool      cropped = false;
                if (TryGetCropRectangle(file, out cropRectangle))
                {
                    try
                    {
                        using (var readStream = new FileStream(file.Path, FileMode.Open))
                        {
                            using (var reader = FormatManager.CreateFormatReader(readStream))
                            {
                                using (var frame = reader.LoadFrame(0))
                                {
                                    if ((cropRectangle.X > 0 || cropRectangle.Y > 0) && (cropRectangle.Width < frame.Width || cropRectangle.Height < frame.Height))
                                    {
                                        using (var resultBitmap = new Bitmap(frame.Width, frame.Height, Aurigma.GraphicsMill.PixelFormat.Format32bppArgb))
                                        {
                                            frame.GetBitmap(resultBitmap);
                                            resultBitmap.Transforms.Crop(cropRectangle);

                                            if (reader.MediaFormat == FormatManager.JpegFormat)
                                            {
                                                resultBitmap.Save(file.Path + ".tmp", new JpegEncoderOptions(95, false));
                                            }
                                            else
                                            {
                                                resultBitmap.Save(file.Path + ".tmp", FormatManager.CreateEncoderOptions(reader.MediaFormat));
                                            }
                                        }
                                        cropped = true;
                                    }
                                }
                            }
                        }

                        if (cropped)
                        {
                            File.Delete(file.Path);
                            File.Move(file.Path + ".tmp", file.Path);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorException(string.Format("Error acquired while cropping file {0}", file.Path), ex);
                    }
                }
            }
            return(new TaskOutput(input.Files, input.Params));
        }