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

时间:2016-03-19 10:37:12

标签: 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;
   using System.Text;
   using System.Windows.Forms;

   namespace WindowsFormsApplication3
   {
     public partial class mainWinForm :Form
    {            
       public mainWinForm()
       {
        InitializeComponent();
        }
      WebCam webcam;

// button2_Click是捕捉图像的按钮

     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:

1 个答案:

答案 0 :(得分:0)

可能的共鸣是你的网络摄像头是空的女巫给出了这个例外: WebCam网络摄像头;

尝试做网络摄像头webcam = new webcam();也提出了一些不必要的论点:)

抱歉我的第一个answear但是当你尝试使用null的东西时会引发null引用因为它没有设置任何希望这对你有所帮助=)