图像保存路径,导致运行时错误

本文关键字:运行时错误 保存 路径 图像 | 更新日期: 2023-09-27 18:36:10

每当我尝试自动保存网络摄像头捕获图像时,都会出现运行时错误,然后 path.automaticaly 名称如 0.jpg,02.jpg,03.jpg这样图像将保存在特定的提及文件夹中,但给出运行时错误。请检查此内容。

namespace camera1
{
    public partial class Form1 : Form
    {
        private Capture capture;
        private bool captureinprogress;
        public Form1()
        {
            InitializeComponent();
        }
        private void ProcessFrame(object sender, EventArgs arg)
        {
           Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
           cameraimage.Image = ImageFrame;
           string root = "C:''photo'0"; // automatically saving image to c drive like       001.jpg,002.jpg;
           for (int i = 0; i < 100; i++)
           {
               if (File.Exists(" "))
               { }
               else
               {
                   string Path = root + i + ".jpg";
                   ImageFrame.Save(Path);
               }
               {
                   if (ImageFrame != null)
                   {
                       pictureBox1.Image = ImageFrame.ToBitmap();
                   }
                   if (pictureBox1 != null)
                   {
                       pictureBox2.Image = ImageFrame.ToBitmap();
                   }
                   if (pictureBox2 != null)
                   {
                       pictureBox3.Image = ImageFrame.ToBitmap();
                   }
             }
         }
     }
     private void btnStart_Click(object sender, EventArgs e)
     {
        if (capture == null)
        {
            try
            {
                capture = new Capture();
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        if (capture != null)
        {
            if (captureinprogress)
            {  //if camera is getting frames then stop the capture and set button Text
                // "Start" for resuming capture
                btnstart.Text = "Start!"; //
                Application.Idle -= ProcessFrame;
            }
            else
            {
                //if camera is NOT getting frames then start the capture and set button
                // Text to "Stop" for pausing capture
                btnstart.Text = "Stop";
                Application.Idle += ProcessFrame;
            }
            captureinprogress = !captureinprogress;        
        }
    }
    private void ReleaseData()
    {
        if (capture != null)
            capture.Dispose();
    }
}
}

图像保存路径,导致运行时错误

你需要屏蔽反斜杠,否则编译器会尝试解释'p oder '0 ,这是他不能

因此,实现此目的的最简单方法是在字符串的开头添加一个@

string root = @"C:'photo'0";

或者您一直使用双反斜杠:

string root = "C:''photo''0";

如需进一步参考,请阅读:

  • http://msdn.microsoft.com/en-us/library/vstudio/as2f1fez.aspx

  • 包含反斜杠的路径字符串的无法识别的转义序列

  • http://msdn.microsoft.com/en-us/library/h21280bw.aspx

我这里没有实例,但您需要将其更改为

string root = @"C:'photo'0'";// instead of string root = "C:''photo'0";

注意:您无法直接保存到 C: 云端硬盘中,因为您需要以管理权限运行程序。我建议使用桌面上的文件夹或某些库文件夹,例如"我的图片"。