// Close current file private void CloseFile( ) { Camera camera = cameraWindow.Camera; if (camera != null) { // detach camera from camera window cameraWindow.Camera = null; // signal camera to stop camera.SignalToStop( ); // wait for the camera camera.WaitForStop( ); camera = null; if (detector != null) { detector.Reset( ); } } if (writer != null) { writer.Dispose( ); writer = null; } intervalsToSave = 0; }
public void DisposeAll() { CloseAll(); currentAvi.Dispose(); preparedAvi.Dispose(); everythingIsGood = false; disposeTimer.Interval = 1000; disposeTimer.Elapsed += new ElapsedEventHandler(disposeTimer_Elapsed); disposeTimer.Enabled = true; while (true) { lock (this) { if (everythingIsGood) { break; } } Thread.Sleep(100); } disposeTimer.Enabled = false; }
private void Camera_NewFrame(object sender, EventArgs e) { if ((intervalsToSave != 0) && (saveOnMotion == true)) { // lets save the frame if (writer == null) { string fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".avi"; try { writer = new AVIWriter("wmv3"); // create AVI writer writer.Open(fileName, _CameraWindow.Camera.Width, _CameraWindow.Camera.Height); // open AVI file } catch (ApplicationException ex) { if (writer != null) { writer.Dispose(); writer = null; } } } Camera camera = _CameraWindow.Camera; // save the frame camera.Lock(); writer.AddFrame(camera.LastFrame); camera.Unlock(); } }
public void Dispose() { if (writer_ != null) { writer_.Close(); writer_.Dispose(); writer_ = null; } }
//private void jpgToAVIByFFpeg(String[] pic, String newPic, int delayTime) //{ // int frameRate = 25; // if (delayTime > 0) // frameRate = Convert.ToInt32(1000f / delayTime); // VideoFileWriter writer = new VideoFileWriter(); // Image im; // if (tVLeft.Nodes.Count > 0) // { // im = Image.FromFile(tVLeft.Nodes[0].Name.ToString()); // int w = im.Width; // int h = im.Height; // progressBar1.Maximum = tVLeft.Nodes.Count; // if (w % 2 != 0) // w += 1; // if (h % 2 != 0) // h += 1; // writer.Open(newPic, w, h, frameRate, VideoCodec.MPEG4); // for (int i = 0; i < tVLeft.Nodes.Count; i++) // { // im.Dispose(); // im = Image.FromFile(tVLeft.Nodes[i].Name.ToString()); // im = BitmapToBlowUp(im, w, h); // this.pictureBox1.Image = im; // this.pictureBox1.Refresh(); // if ((im.Height != h) | (im.Width != w)) // MessageBox.Show(tVLeft.Nodes[i].Name.ToString() + " 文件大小与首张不符!"); // writer.WriteVideoFrame((Bitmap)im); // progressBar1.Value += 1; // curFile.Text = i.ToString() + ">>> " + getFileNameOnly(pic[i]); // curFile.Refresh(); // } // writer.Close(); // writer.Dispose(); // MessageBox.Show("AVI 动画文件转换完毕!"); // } //} // FFPEG 类生成AVI,使用 MPG4 格式压缩 // 如果使用 FFPEG 类,则需要: // 1、引用如下类库: // AForge.Video.FFMPEG // 2、将如下类库 Copy 到目标程度所在文件夹,如 bin\debug 等。 // AForge.Net FrameWork \Externals\FFMPEG\BIN文件夹下的所有 DLL 文件 // avcodec-53.dll,avdevice-53.dll,avfilter-2.dll,avformat-53.dll,avutil-51.dll,postproc-52.dll,swresample-0.dll,swscale-2.dll private void jpgToAVI(String[] pic, String newPic, int delayTime) { int frameRate = 25; if (delayTime > 0) { frameRate = Convert.ToInt32(1000f / delayTime); } AVIWriter aviWriter = new AVIWriter("wmv3"); aviWriter.FrameRate = frameRate; Image im; if (tVLeft.Nodes.Count > 0) { im = Image.FromFile(tVLeft.Nodes[0].Name.ToString()); int w = im.Width; int h = im.Height; progressBar1.Maximum = tVLeft.Nodes.Count; if (w % 2 != 0) { w += 1; } if (h % 2 != 0) { h += 1; } aviWriter.Open(newPic, w, h); // 输出到指定的文件 for (int i = 0; i < tVLeft.Nodes.Count; i++) { im.Dispose(); im = Image.FromFile(tVLeft.Nodes[i].Name.ToString()); im = BitmapToBlowUp(im, w, h); this.pictureBox1.Image = im; this.pictureBox1.Refresh(); if ((im.Height != h) | (im.Width != w)) { MessageBox.Show(tVLeft.Nodes[i].Name.ToString() + " 文件大小与首张不符!"); } aviWriter.AddFrame((Bitmap)im); progressBar1.Value += 1; curFile.Text = i.ToString() + ">>> " + getFileNameOnly(pic[i]); curFile.Refresh(); } aviWriter.Close(); aviWriter.Dispose(); MessageBox.Show("AVI 动画文件转换完毕!"); } } // WMV3 格式压缩
private void StartConsumerThread() { if (!IsWorking) { IsWorking = true; Helpers.CreateDirectoryIfNotExist(OutputPath); AVIWriter aviWriter = new AVIWriter(OutputPath, FPS, Size.Width, Size.Height, ShowOptions); task = TaskEx.Run(() => { try { position = 0; while (!imageQueue.IsCompleted) { Image img = null; try { img = imageQueue.Take(); if (img != null) { //img.Save("Test\\" + position + ".bmp", ImageFormat.Bmp); aviWriter.AddFrame((Bitmap)img); position++; } } catch (InvalidOperationException) { } finally { if (img != null) { img.Dispose(); } } } } finally { IsWorking = false; if (aviWriter != null) { aviWriter.Dispose(); } } }); } }
public void Dispose() { if (videoStream != null) { videoStream.Close(); videoStream.Dispose(); } else { videoWriter.Close(); videoWriter.Dispose(); } }
/// <summary> /// 开启摄像头前,先关闭资源 /// </summary> void CloseFile() { Camera camera = MainWindow.CameraWindow.Camera; if (camera != null) { MainWindow.CameraWindow.Camera = null; camera.SignalToStop(); camera.WaitForStop(); camera = null; if (detector != null) { detector.Reset(); } } if (writer != null) { writer.Dispose(); writer = null; } }
private void CloseFile() { Camera camera = _CameraWindow.Camera; if (camera != null) { _CameraWindow.Camera = null; // detach camera from camera window camera.Stop(); camera = null; if (_MotionDetector != null) { _MotionDetector.Reset(); } } if (writer != null) { writer.Dispose(); writer = null; } intervalsToSave = 0; }
// On new frame private void Camera_NewFrame(object sender, System.EventArgs e) { if ((intervalsToSave != 0) && (saveOnMotion == true)) { // lets save the frame if (writer == null) { // create file name DateTime date = DateTime.Now; String fileName = String.Format("{0}-{1}-{2} {3}-{4}-{5}.avi", date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second); try { // create AVI writer writer = new AVIWriter("wmv3"); // open AVI file writer.Open(fileName, cameraWindow.Camera.Width, cameraWindow.Camera.Height); } catch (ApplicationException) { if (writer != null) { writer.Dispose( ); writer = null; } } } // save the frame Camera camera = cameraWindow.Camera; camera.Lock( ); writer.AddFrame(camera.LastFrame); camera.Unlock( ); } }
//关闭录制 public void RecordClose() { Thread.Sleep(500); // 防止在关闭时,aviWriter.AddFrame(lastFrame);报错 aviWriter.Close(); aviWriter.Dispose(); }
private void StartConsumerThread() { if (!IsWorking) { IsWorking = true; Helpers.CreateDirectoryIfNotExist(OutputPath); AVIWriter aviWriter = new AVIWriter(OutputPath, FPS, Size.Width, Size.Height, ShowOptions); task = TaskEx.Run(() => { try { position = 0; while (!imageQueue.IsCompleted) { Image img = null; try { img = imageQueue.Take(); if (img != null) { //img.Save("Test\\" + position + ".bmp", ImageFormat.Bmp); aviWriter.AddFrame((Bitmap)img); position++; } } catch (InvalidOperationException) { } finally { if (img != null) img.Dispose(); } } } finally { IsWorking = false; if (aviWriter != null) { aviWriter.Dispose(); } } }); } }
public bool Start(int framesPerSecond) { if (recordingWindow != null) { string windowTitle = Regex.Replace(recordingWindow.Text, @"[^\x20\d\w]", ""); if (string.IsNullOrEmpty(windowTitle)) { windowTitle = "greenshot-recording"; } filename = Path.Combine(conf.OutputFilePath, windowTitle + ".avi"); } else { filename = Path.Combine(conf.OutputFilePath, "greenshot-recording.avi"); } if (File.Exists(filename)) { try { File.Delete(filename); } catch {} } LOG.InfoFormat("Capturing to {0}", filename); if (recordingWindow != null) { LOG.InfoFormat("Starting recording Window '{0}', {1}", recordingWindow.Text, recordingWindow.WindowRectangle); recordingSize = recordingWindow.WindowRectangle.Size; } else { LOG.InfoFormat("Starting recording rectangle {0}", recordingRectangle); recordingSize = recordingRectangle.Size; } if (recordingSize.Width % ALIGNMENT > 0) { LOG.InfoFormat("Correcting width to be factor alignment, {0} => {1}", recordingSize.Width, recordingSize.Width + (ALIGNMENT - (recordingSize.Width % ALIGNMENT))); recordingSize = new Size(recordingSize.Width + (ALIGNMENT - (recordingSize.Width % ALIGNMENT)), recordingSize.Height); } if (recordingSize.Height % ALIGNMENT > 0) { LOG.InfoFormat("Correcting Height to be factor alignment, {0} => {1}", recordingSize.Height, recordingSize.Height + (ALIGNMENT - (recordingSize.Height % ALIGNMENT))); recordingSize = new Size(recordingSize.Width, recordingSize.Height + (ALIGNMENT - (recordingSize.Height % ALIGNMENT))); } this.framesPerSecond = framesPerSecond; // "P/Invoke" Solution for capturing the screen hWndDesktop = User32.GetDesktopWindow(); // get te hDC of the target window hDCDesktop = User32.GetWindowDC(hWndDesktop); // Make sure the last error is set to 0 Win32.SetLastError(0); // create a device context we can copy to hDCDest = GDI32.CreateCompatibleDC(hDCDesktop); // Check if the device context is there, if not throw an error with as much info as possible! if (hDCDest == IntPtr.Zero) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateCompatibleDC", recordingSize); // Cleanup User32.ReleaseDC(hWndDesktop, hDCDesktop); // throw exception throw exceptionToThrow; } // Create BitmapInfoHeader for CreateDIBSection BitmapInfoHeader bitmapInfoHeader = new BitmapInfoHeader(recordingSize.Width, recordingSize.Height, 32); // Make sure the last error is set to 0 Win32.SetLastError(0); // create a bitmap we can copy it to, using GetDeviceCaps to get the width/height hDIBSection = GDI32.CreateDIBSection(hDCDesktop, ref bitmapInfoHeader, BitmapInfoHeader.DIB_RGB_COLORS, out bits0, IntPtr.Zero, 0); if (hDIBSection == IntPtr.Zero) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateDIBSection", recordingSize); exceptionToThrow.Data.Add("hdcDest", hDCDest.ToInt32()); exceptionToThrow.Data.Add("hdcSrc", hDCDesktop.ToInt32()); // clean up GDI32.DeleteDC(hDCDest); User32.ReleaseDC(hWndDesktop, hDCDesktop); // Throw so people can report the problem throw exceptionToThrow; } // Create a GDI Bitmap so we can use GDI and GDI+ operations on the same memory GDIBitmap = new Bitmap(recordingSize.Width, recordingSize.Height, 32, PixelFormat.Format32bppArgb, bits0); // select the bitmap object and store the old handle hOldObject = GDI32.SelectObject(hDCDest, hDIBSection); stop = false; aviWriter = new AVIWriter(); // Comment the following 2 lines to make the user select it's own codec //aviWriter.Codec = "msvc"; //aviWriter.Quality = 99; aviWriter.FrameRate = framesPerSecond; if (aviWriter.Open(filename, recordingSize.Width, recordingSize.Height)) { // Start update check in the background backgroundTask = new Thread (new ThreadStart(CaptureFrame)); backgroundTask.IsBackground = true; backgroundTask.Name = "Capture video"; backgroundTask.Start(); return true; } else { // Cancel aviWriter.Dispose(); aviWriter = null; } return false; }
public static void OverAvi() { avi.Close(); avi.Dispose(); avi = null; }