Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 制作一个按钮,将我的图片保存在一个从文本框中获取其名称的文件夹中_C#_Winforms - Fatal编程技术网

C# 制作一个按钮,将我的图片保存在一个从文本框中获取其名称的文件夹中

C# 制作一个按钮,将我的图片保存在一个从文本框中获取其名称的文件夹中,c#,winforms,C#,Winforms,我正试图为我工作的汽车零售公司制定一个计划。当人们交付汽车时,一个人必须用联想miix拍摄损坏的照片 到目前为止我做的程序还不够聪明。 就像,你把数字板写在一个盒子里,然后用盒子里的文本创建一个文件夹 然后启动相机,拍摄照片并保存,然后手动查找文件夹并命名文件 有没有办法让它更智能,只需两个按钮,一个启动相机,另一个拍照,然后自动保存在名为数字板的文件夹中,以及名为1、2、3、4等的文件。jpg 这是我目前的代码: namespace Europcar_skade_camera { p

我正试图为我工作的汽车零售公司制定一个计划。当人们交付汽车时,一个人必须用联想miix拍摄损坏的照片

到目前为止我做的程序还不够聪明。 就像,你把数字板写在一个盒子里,然后用盒子里的文本创建一个文件夹

然后启动相机,拍摄照片并保存,然后手动查找文件夹并命名文件

有没有办法让它更智能,只需两个按钮,一个启动相机,另一个拍照,然后自动保存在名为数字板的文件夹中,以及名为1、2、3、4等的文件。jpg

这是我目前的代码:

namespace Europcar_skade_camera
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private FilterInfoCollection webcam;
        private VideoCaptureDevice cam;

        private void Form1_Load(object sender, EventArgs e)
        {
            webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach(FilterInfo VideoCaptureDevice in webcam)
            {
                comboBox1.Items.Add(VideoCaptureDevice.Name);
            }
            comboBox1.SelectedIndex = 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
            cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
            cam.Start();
        }

        private void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = bit;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (cam.IsRunning)
            {
                cam.Stop();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = @"C:\tmp\";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image.Save(saveFileDialog1.FileName);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void nummerplade_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Directory.CreateDirectory(@"c:\tmp\" + nummerplade.Text);
        }
    }
}
大概是这样的:

var filePath = System.IO.Path.Combine(@"c:\tmp", nummerplade.Text, fileNumber + ".jpeg");
您不必使用SaveFileDialog,只需根据铭牌值创建文件路径,如下所示:

var filePath = System.IO.Path.Combine(@"c:\tmp", nummerplade.Text, fileNumber + ".jpeg");
fileNumber
var是文件夹+1中准备就绪的文件数

var fileNumber=Directory.GetFiles(…).Length+1

string numberPlate = "21323412";
string path = @"C:\tmp\";

Directory.CreateDirectory(path + numberPlate); //to create folder
string dirpath = path + numberPlate; //to get name of fodler we created
您还提到您需要增量名称,有很多方法可以做到这一点,就个人而言,我更喜欢文件的大哈希名称,如:

//generate GUID and convert to string.
string filename = Guid.NewGuid().ToString();

//to save picturebox image in folder and use ImageFormat.Your desired format
pictureBox1.Image.Save(dirpath + @"\"+filename+".jpeg", ImageFormat.Jpeg); 
但如果您想坚持使用增量名称:

// you get last name in the folder:

//it gets last filename in folder but with path
var lastFilePath = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First(); 

//we remove path from filename and convert it to int.
int lastIndex = Int32.Parse(Path.GetFileNameWithoutExtension(lastFilePath)); 

//then we increment it by 1
int newName = lastIndex + 1;

//and save it
pictureBox1.Image.Save(dirpath + @"\"+filename+".jpeg", ImageFormat.Jpeg);  //to save picturebox image in folder
使用GUID时,最后应该是这样的:

    string numberPlate = "21323412";
    string path = @"C:\tmp\";

    private void button2_Click(object sender, EventArgs e)
    {
        Directory.CreateDirectory(path + numberPlate); //to create folder
        string dirpath = path + numberPlate; //to get name of fodler we created
        string filename = Guid.NewGuid().ToString();
        pictureBox1.Image.Save(dirpath + @"\"+filename+".jpeg", ImageFormat.Jpeg); //to save picturebox image in folder

    }

如果您在框中写入注册铭牌,难道您不能仅使用该文本作为文件名吗?您的按钮4_单击事件正在为您构建目录结构,请对此进行修改,同时将路径另存为应用程序中的局部变量,这样您就可以轻松访问该文件夹。要自动为图像编号,拍照时,增加一个数字并分配给图像,拍照次数越多,数字越高,直到您关闭应用程序更改数字以清除为止plate@uk2k05若他在还没拍完汽车的所有照片时意外关闭了应用程序,那个该怎么办?增量应该保存在某个地方。@Peter我在回答中做了一些更改,如果您有任何其他错误,请告诉我。谢谢您尝试帮助我:)!。。现在我得到一个错误:“ImageFormat”名称在当前上下文中不存在是,因为需要使用System.Drawing.Imaging添加;在文件的顶部:)谢谢@Nick:)。我现在按下按钮时出错@Peter你忘了更改我使用的“路径”,例如:D你应该使用你的路径,文件夹将在哪里。例如:c:\myfolder\itwillshore