简体   繁体   English

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

[英]how do I handle External Exceptions in my code

I am trying to capture image on the count of a timer and save it automatically for which I have tried a code But an External Exception is been thrown when I run this code.And if i try to catch the exception the code runs but it doesn't save the captured image neither in the mentioned path nor anywhere else. 我正在尝试捕获计时器的图像并自动保存为我尝试过的代码,但是当我运行此代码时会抛出一个外部异常。如果我尝试捕获该异常,则代码会运行但它不会既不将捕获的图像保存在提到的路径中,也不保存在其他任何地方。 Here picCap is the name of my pictureBox 这里picCap是我的pictureBox的名称

My code is, 我的代码是

    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);
    }

Extra details about the exception 有关异常的更多详细信息

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: 

The exception indicates that the method requires a filename and not a folder path: 异常表明该方法需要文件名而不是文件夹路径:

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

It's likely failing because it can't save the file without the filename, so try adding one like the following example: 可能由于未保存文件名而无法保存文件而失败,因此请尝试添加一个类似于以下示例的文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM