参数异常是在c#中捕获的 [英] argument exception was caught in c#

查看:60
本文介绍了参数异常是在c#中捕获的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public string FindFace()
        {
           
            string ex = Path.GetExtension(lbFileName.Text);
            var uniqueName = Guid.NewGuid();
            
            var myUniqueFileName = string.Format(@"{0}{1}", uniqueName, ex);
            Console.WriteLine("Answer ",myUniqueFileName);
            Bitmap InputImage = new Bitmap(lbFileName.Text);
            InputImage.Save(path + myUniqueFileName);


            //System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(myUniqueFileName);
            //Image img = (Image)converter.ConvertFrom(byteArrayIn);

            
            
            using (Image<Bgr, Byte> image = new Image<Bgr, byte>(myUniqueFileName))
            {
                HaarCascade haar = new HaarCascade("haarcascade_frontalface_alt2.xml");
                // there's only one channel (greyscale), hence the zero index
                //var faces = nextFrame.DetectHaarCascade(haar)[0];
                Image<Gray, byte> grayframe = image.Convert<Gray, byte>();
                var faces =
                        grayframe.DetectHaarCascade(
                                haar, 1.4, 4,
                                HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                new Size(image.Width / 25, image.Height / 25)
                                )[0];

                if (faces.Length != 0)
                {

                   
                    //foreach (var face in faces)
                    //{
                        var FaceFileName = string.Format(@"{0}{1}", uniqueName, ex);
                        // image.Draw(face.rect, new Bgr(0, 255, 0), 3);
                        image.ROI = faces[0].rect;

                        string FaceImage = path + "Temp\\" + FaceFileName;
                        image.Save(FaceImage);

                        ImgFace.Image = Image.FromFile(FaceImage);


                        MessageBox.Show("Face Detected", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   

                    //}
                   
                }
                else
                {

                    MessageBox.Show("No Face In this Image", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    lbFileName.Text = "No File Choosen";
                    this.pictureBox1.BackgroundImage = global::FaceAnnotation.Properties.Resources.no_image_selected;
                }
                File.Delete(path + myUniqueFileName);

                return uniqueName.ToString();
               
            }

        }

推荐答案

构建时的最佳实践文件系统路径是使用专用方法:

The best practice when it comes to build file system paths is to use a dedicated method:
string result = Path.Combine(path, myUniqueFileName);



这将使您获得有效的路径规范。

更多信息: MSDN:路径类 [ ^ ]


这篇关于参数异常是在c#中捕获的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆