2013-04-30 23 views
0

我在C#窗体中创建了一个程序我的窗体中有很多函数,其中包括两个连接到dabase的datagrid视图,包括一个直接连接到我的PC的摄像机我使用AForge DLL参考连接到我刚刚找到的摄像机设备教程youtube,它对我来说是完美的,正如我前面说过的,我在一个表单中包含了太多的程序,包括那个相机,并且它出去了,相机需要调整到一个小的分辨率,所以我决定做一个弹出当我点击表单上的按钮时,该按钮必须显示更宽的分辨率。如何在点击按钮时使用图片框弹出相机?

这是我的相机的代码。

//Camera 

    // get the devices name 
    private void getCamList() 
    { 
     try 
     { 
      videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 
      comboBox1.Items.Clear(); 
      if (videoDevices.Count == 0) 
       throw new ApplicationException(); 

      DeviceExist = true; 
      foreach (FilterInfo device in videoDevices) 
      { 
       comboBox1.Items.Add(device.Name); 
      } 
      comboBox1.SelectedIndex = 0; //make dafault to first cam 
     } 
     catch (ApplicationException) 
     { 
      DeviceExist = false; 
      comboBox1.Items.Add("No capture device on your system"); 
     } 
    } 

    //refresh button 
    private void refresh_Click(object sender, EventArgs e) 
    { 
     getCamList(); 
    } 

    //toggle start and stop button 
    private void start_Click(object sender, EventArgs e) 
    { 
     if (start.Text == "&Start") 
     { 
      if (DeviceExist) 
      { 
       videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); 
       videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); 
       CloseVideoSource(); 
       videoSource.DesiredFrameSize = new Size(160, 120); 
       //videoSource.DesiredFrameRate = 10; 
       videoSource.Start(); 
       lblCam.Text = "Device running..."; 
       start.Text = "&Stop"; 
      } 
      else 
      { 
       lblCam.Text = "Error: No Device selected."; 
      } 
     } 
     else 
     { 
      if (videoSource.IsRunning) 
      { 
       CloseVideoSource(); 
       lblCam.Text = "Device stopped."; 
       start.Text = "&Start"; 
      } 
     } 
    } 

    //eventhandler if new frame is ready 
    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) 
    { 
     Bitmap img = (Bitmap)eventArgs.Frame.Clone(); 
     //do processing here 
     pictureBox1.Image = img; 
    } 

    //close the device safely 
    private void CloseVideoSource() 
    { 
     if (!(videoSource == null)) 
      if (videoSource.IsRunning) 
      { 
       videoSource.SignalToStop(); 
       videoSource = null; 
      } 
    } 

    //prevent sudden close while device is running 
    private void Form1_FormClosed(object sender, FormClosingEventArgs e) 
    { 
     CloseVideoSource(); 
    } 


} } 

我也贴一张照片,让您进一步了解我在说什么。 enter image description here 正如你可以看到在右下角我有一个弹出按钮在那里老实告诉你我已经尝试过不同的方法,但没有工作不幸的是我不能发布我试过,因为我昨天创建它,并不能再撤消我试过的代码。任何想法?

+0

实际上是否存在一个缺少标点符号的混乱问题? – 2013-04-30 13:07:16

回答

0
  • 创建一个新的窗体
  • 将一个PictureBox这个表格
  • 添加的所有方法来初始化,并获得当前帧形式(应当重构为一个自己的类,它提供像FrameChanged给你一个事件当前图像)
  • 像添加一个方法来表单
     
    public void ShowCamPopup(string deviceName) 
    { 
        InitializeDevice(string deviceName, int width, int height); 
        this.Show(); 
        this.BringToTop(); 
    } 
    

你应该真正考虑重构与凸轮的通信自己的类,它可以减少重复代码,并允许您在解决方案的单个位置执行性能调整(稍后您将需要)。

+0

我昨天走了,我也发现我应该只能一次运行一个摄像头,所以如果我弹出另一个显示另一个摄像头的窗体,那么这个窗体将最终不工作或运行,所以也许当我点击弹出窗口时,我也应该在另一个人开始之前在我的主表单上终止原始相机? – user2262382 2013-04-30 13:25:16

+0

在这种情况下,我会初始化一个摄像头拍摄更大尺寸的图像(以便您可以将其显示在窗体上)。小表单上的图片框可以相应地调整图片的大小。在newFrame事件处理程序中将图片设置为弹出窗口。 – wonko79 2013-04-30 13:33:54

相关问题