public AutoCrop(string filename, UserSettings settings, ref CropInfo cropInfo) { InitializeComponent(); try { this.settings = settings; this.cropInfo = cropInfo; AviSynthScriptEnvironment asse = new AviSynthScriptEnvironment(); asc = asse.OpenScriptFile(filename); if(asc.HasVideo) { System.Drawing.Size s = new Size(asc.VideoWidth, asc.VideoHeight); this.ClientSize = s; maxFrames = asc.num_frames; // remove last ~10 minutes if (maxFrames > 30000) { maxFrames -= 15000; } numericUpDownFrame.Maximum = maxFrames; int step = 0; step = maxFrames / nrFrames; curFrame = step; Monitor.Enter(drawLock); bitmap = ReadFrameBitmap(asc, step); bitmapCopy = new Bitmap(bitmap); Monitor.Exit(drawLock); } } catch (Exception ex) { cropInfo.error = true; cropInfo.errorStr = ex.Message; } }
public Bitmap ReadFrameBitmap(AviSynthClip asc, int position) { Bitmap bmp = null; try { bmp = new Bitmap(asc.VideoWidth, asc.VideoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // Lock the bitmap's bits. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); try { // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Read data asc.ReadFrame(ptr, bmpData.Stride, position); } finally { // Unlock the bits. bmp.UnlockBits(bmpData); } bmp.RotateFlip(RotateFlipType.Rotate180FlipX); return bmp; } catch (Exception) { if(bmp != null) bmp.Dispose(); throw; } }