C# 我无法使用c代码在窗口窗体应用程序中捕获图像

C# 我无法使用c代码在窗口窗体应用程序中捕获图像,c#,.net,C#,.net,我已经编写了一个捕获图像的代码,输入0来捕获图像。该代码似乎正在运行,但在给定的输入下它不会捕获任何图像 以下是代码 'using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq;

我已经编写了一个捕获图像的代码,输入0来捕获图像。该代码似乎正在运行,但在给定的输入下它不会捕获任何图像

以下是代码

  'using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Drawing.Imaging;
   using System.Linq;
   using System.Text;
   using System.Windows.Forms;

   namespace WindowsFormsApplication3
   {
     public partial class mainWinForm :Form
    {            
       public mainWinForm()
       {
        InitializeComponent();
        }
      WebCam webcam;
//按钮2\u单击是捕获图像的按钮

     private void button2_Click(object sender, EventArgs e)
     {
         int a = Convert.ToInt32(textBox1.Text);//textbox to give i/p
         Console.WriteLine(a);

         try
         {


             if (a == 0)
             {
                 webcam.Start();
                 picCap.Image.Save(@"C:\Users\Administrator\Desktop\temp\MyImage.jpg", ImageFormat.Jpeg);

             }
             else
             {
                 MessageBox.Show("Wrong input");

             }

             webcam.Stop();
         }
         catch (NullReferenceException)
         {
             return;
         }             
     }

     private void mainWinForm_Load(object sender, EventArgs e)
     {
         webcam = new WebCam();
         webcam.InitializeWebCam(ref picCap);
     }
//picCap_Click是必须捕获图像的图片框

    private void picCap_Click(object sender, EventArgs e)
     {

     }

}
}'
例外情况详情: 生成空引用异常

System.NullReferenceException was unhandled
 HResult=-2147467261
 Message=Object reference not set to an instance of an object.
 Source=WindowsFormsApplication3
 StackTrace:
   at WindowsFormsApplication3.mainWinForm.button2_Click(Object sender, EventArgs e) in C:\Users\Administrator\documents\visual studio 2010\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs:line 49
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WindowsFormsApplication3.Program.Main() in C:\Users\Administrator\documents\visual studio 2010\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException:

可能的原因是您的网络摄像头为空,这会导致以下异常: 网络摄像机

尝试执行webcam webcam=新建webcam();同时加入nessecary参数:)


对不起,我的第一个answear,但是当您尝试使用空值的东西时,会抛出空引用,因为它没有设置为任何值(希望这对您有所帮助=)

您是否遇到任何异常?是的,存在空引用异常。但我已尝试捕获异常。。。这难道不应该解决这个问题吗?好吧,让我们结束这个-奥斯瓦尔德,当你可以遵守网站规则时,请随意提问。其中包括完整的BUG报告。其中包括错误详细信息。如果你没有足够的C#,那么你需要开始做一些真正基础的学习。我已经在mainWinForm#Load()函数中提供了它