private void btnDungHinh_Click(object sender, EventArgs e) { if (camStatus) { try { if (capture == null) { MessageBox.Show("Chưa kết nối thiết bị thu video. Vui lòng kiểm tra lại camera.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } heFrame = new Capture.HeFrame(CaptureComplete); capture.FrameEvent2 += heFrame; capture.GrapImg(); capture.PreviewWindowFrame = null; pbVideo.Image = imgCapture; btnDungHinh.Text = "Tiếp tục"; btnDungHinh.Image = Properties.Resources.continue_24; btnDungHinh.ImageAlign = ContentAlignment.MiddleLeft; camStatus = false; } catch (Exception ex) { MessageBox.Show("Dừng hình thất bại. Vui lòng kiểm tra lại thông số cài đặt video!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); initCamera(pbVideo); Console.WriteLine(ex.Message + "\n\n" + ex.ToString()); } } else { if (capture == null) { MessageBox.Show("Chưa kết nối thiết bị thu video. Vui lòng kiểm tra lại kết nối camera!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } initCamera(pbVideo); camStatus = true; btnDungHinh.Text = "Dừng hình"; btnDungHinh.Image = Properties.Resources.pause_24; btnDungHinh.ImageAlign = ContentAlignment.MiddleLeft; } }
public bool initCamera(PictureBox pBox) { bool isSucess = false; if (capture != null) { capture.Dispose(); } //Init with config param loaded from DB try { //Load config from DB string videoDevice = Settings.Default.vDevice; string videoCompressor = Settings.Default.vCompress; filters = new Filters(); if ((videoDevice != null) && (videoDevice != "")) { capture = new Capture(new Filter(videoDevice)); //set video compressor if ((videoCompressor != null) && (videoCompressor != "")) { try { capture.VideoCompressor = new Filter(videoCompressor); }catch (Exception ex) { Console.WriteLine(ex.ToString()); capture.VideoCompressor = null; } } capture.PreviewWindowFrame = pBox; isSucess = true; } else { isSucess = false; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); isSucess = false; } if (isSucess == false) { //Auto reinit camera with default param try { if (filters != null) { capture = new Capture(filters.VideoInputDevices[0]); capture.PreviewWindow = pBox; //Save new config param Settings.Default.vDevice = capture.VideoDevice.MonikerString; if (capture.VideoCompressor != null) { Settings.Default.vCompress = capture.VideoCompressor.MonikerString; } Settings.Default.Save(); isSucess = true; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); isSucess = false; } } //init for capture frame if (isSucess) { try { heFrame = new Capture.HeFrame(CaptureComplete); capture.FrameEvent2 += heFrame; capture.GrapImg(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return(isSucess); }