public ImageCropper(string[] files, int skipRate, VideoSize size, int startX, int endX, int startY, int endY, double startZoom, double endZoom, string directory)
 {
     mFiles     = files;
     mSkipRate  = skipRate;
     mVideoSize = size;
     mStartX    = startX;
     mEndX      = endX;
     mStartY    = startY;
     mEndY      = endY;
     mStartZoom = startZoom;
     mEndZoom   = endZoom;
     mDirectory = directory;
 }
        public void UpdateOutputStatus()
        {
            int numInputs  = mFiles.Length;
            int skipRate   = (int)ImageSkip.Value;
            int numOutputs = numInputs / skipRate;

            double imageInterval = (double)ImageInterval.Value;
            double frameRate     = (double)VideoFrameRate.Value;
            double speedupFactor = imageInterval * frameRate * skipRate;

            TimeSpan originalDuration = new TimeSpan(0, 0, (int)(numInputs * imageInterval));
            TimeSpan outputDuration   = new TimeSpan(0, 0, (int)(numInputs * imageInterval / speedupFactor));

            VideoSize vidSize = mVideoSizes[VideoSize.SelectedIndex];

            outputsLabel.Text = string.Format("Outputs:\n{0} frames\n{1} x {2}", numOutputs, vidSize.Width, vidSize.Height);

            calculatorLabel.Text = string.Format("Timing:\nOrig: {0}\nOut: {1}\nSpeedup: {2}x", originalDuration, outputDuration, speedupFactor);
        }
        public void DrawImage(bool drawOnStartImage)
        {
            PictureBox pictureBox = EndImage;
            string     filename   = mFiles[mFiles.Length - 1];
            int        xOffset    = (int)EndXOffset.Value;
            int        yOffset    = (int)EndYOffset.Value;
            double     zoom       = (double)EndZoom.Value;
            VideoSize  vidSize    = mVideoSizes[VideoSize.SelectedIndex];

            if (drawOnStartImage)
            {
                pictureBox = StartImage;
                filename   = mFiles[0];
                xOffset    = (int)StartXOffset.Value;
                yOffset    = (int)StartYOffset.Value;
                zoom       = (double)StartZoom.Value;
            }

            Image  inputImage   = Image.FromFile(filename);
            double inputRatio   = (double)inputImage.Width / inputImage.Height;
            int    outputHeight = (int)(pictureBox.Size.Width / inputRatio);
            double imageZoom    = (double)inputImage.Width / pictureBox.Size.Width;

            if (drawOnStartImage)
            {
                InputStatus.Text = string.Format("{0} input files at {1}x{2}", mFiles.Length, inputImage.Width, inputImage.Height);
            }

            Bitmap   bm   = new Bitmap(inputImage, pictureBox.Size.Width, outputHeight);
            Graphics grap = Graphics.FromImage(bm);

            grap.InterpolationMode = InterpolationMode.HighQualityBicubic;

            Rectangle rect = new Rectangle((int)(xOffset / imageZoom), (int)(yOffset / imageZoom), (int)(vidSize.Width * zoom / imageZoom), (int)(vidSize.Height * zoom / imageZoom));

            grap.DrawRectangle(Pens.Red, rect);

            pictureBox.Image = bm;
        }
        private void DoCropping()
        {
            string directory = string.Format("{0}\\Cropped {1}x", mDirectory, mRealtimeMultiplier);
            int    appender  = 0;

            while (Directory.Exists(directory))
            {
                appender++;
                directory = string.Format("{0}\\Cropped {1}x_{2}", mDirectory, mRealtimeMultiplier, appender);
            }
            Directory.CreateDirectory(directory);

            int numInputs  = mFiles.Length;
            int numOutputs = numInputs / mSkipRate;

            //Parallel.For(0, numOutputs, (i) => {
            for (int i = 0; i < numOutputs; i++)
            {
                ClipInfo clipInfo = null;
                if (mClipInfos != null)
                {
                    clipInfo = mClipInfos[i * mSkipRate];
                }

                if (Event != null)
                {
                    Event(this, new CropperEventArgs(i, numOutputs, null));
                }

                int    xOffset = clipInfo != null ? clipInfo.Offset.X : (int)(mStartX + ((mEndX - mStartX) * i / (numOutputs - 1)));
                int    yOffset = clipInfo != null ? clipInfo.Offset.Y : (int)(mStartY + ((mEndY - mStartY) * i / (numOutputs - 1)));
                double zoom    = clipInfo != null ? clipInfo.Zoom : (double)(mStartZoom + ((mEndZoom - mStartZoom) * i / (numOutputs - 1)));
                if (clipInfo != null)
                {
                    mVideoSize = clipInfo.FrameSize;
                }

                //Get the output filename
                string outName = string.Format("{0}\\{1}_{2}x_{3:00000}.png", directory, mLabel, mRealtimeMultiplier, i);

                Console.WriteLine(string.Format("Processing frame {0}, ({1}, {2}), output {3}", i, xOffset, yOffset, outName));

                //Crop the image
                using (Bitmap bmpImage = new Bitmap(mFiles[i * mSkipRate]))
                {
                    using (Bitmap bmpCrop = bmpImage.Clone(new Rectangle(xOffset, yOffset, (int)(mVideoSize.Width * zoom), (int)(mVideoSize.Height * zoom)), bmpImage.PixelFormat))
                    {
                        using (Bitmap bm = new Bitmap(bmpCrop, mVideoSize.Width, mVideoSize.Height))
                        {
                            Graphics grap = Graphics.FromImage(bm);
                            grap.InterpolationMode = InterpolationMode.HighQualityBicubic;

                            //Save the image
                            bm.Save(outName);
                            grap.Dispose();

                            if (Event != null)
                            {
                                Event(this, new CropperEventArgs(i, numOutputs, bm));
                            }
                        }
                    }
                }
            }
            //});

            if (Event != null)
            {
                Event(this, new CropperEventArgs(numOutputs, numOutputs, null));
            }
        }