如何在我的代码中处理外部异常

时间:2016-02-11 11:39:35

标签: c# .net

我正在尝试捕获计时器计数的图像并保存自动我已经尝试过代码但是当我运行此代码时抛出了外部异常。如果我尝试捕获代码运行的异常但它不会在所提到的路径中或其他任何地方保存捕获的图像。 这里picCap是我的pictureBox的名称

我的代码是,

    WebCam webcam;
    int duration = 0;

    private void timer1_Tick(object sender, EventArgs e)
    {
        duration++;
        textBox1.Text = duration.ToString();
        bool ischecked = radioButton2.Checked;

            if (duration <= 3)
            {
                radioButton2.Checked = true;
                if (radioButton2.Checked == true)
                {


                    picCap.Image.Save(@"C:\Users\Administrator\Desktop", ImageFormat.Jpeg);
                   //External Exception not handled

                }
                else
                {
                    webcam.Stop();
                    timer1.Stop();
                }

            }
            else
            {
                radioButton2.Checked = false;
                //     radioButton3.Visible = false;
                radioButton3.Checked = true;

            }


    }
    //start
    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        timer1.Start();
        webcam.Start();
    }
    //stop
    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        webcam.Stop();
    }

    private void mainWinForm_Load(object sender, EventArgs e)
    {
        webcam = new WebCam();
        webcam.InitializeWebCam(ref picCap);
    }

有关例外的额外详情

System.Runtime.InteropServices.ExternalException was unhandled by user code
HResult=-2147467259
Message=A generic error occurred in GDI+.
Source=System.Drawing
ErrorCode=-2147467259
 StackTrace:
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(String filename, ImageFormat format)
   at WindowsFormsApplication3.mainWinForm.timer1_Tick(Object sender, EventArgs e) in C:\Users\Administrator\documents\visual studio 2010\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs:line 37
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.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.DoEvents()
   at WebCam_Capture.WebCamCapture.timer1_Tick(Object sender, EventArgs e)
 InnerException: 

1 个答案:

答案 0 :(得分:1)

异常表示该方法需要文件名而不是文件夹路径:

System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)

它可能会失败,因为它无法在没有文件名的情况下保存文件,因此请尝试添加如下示例:

picCap.Image.Save(@"C:\Users\Administrator\Desktop\MyFilename.jpeg", ImageFormat.Jpeg);