简体   繁体   English

使用 Emgu CV 将图像保存到文件夹

[英]Save images to folder using Emgu CV

I have a few images which are extracted from other images using Emgu CV .我有一些使用Emgu CV从其他图像中提取的图像。

I've tried to save these images into a folder, but only last image is being saved to folder with my code.我试图将这些图像保存到一个文件夹中,但只有最后一张图像与我的代码一起保存到文件夹中。

 Image.ToBitmap().save("filename",imageformat.png)

How I could save all images in one folder?如何将所有图像保存在一个文件夹中?

You can try doing something like this.你可以尝试做这样的事情。 I have taken the name of the file and I'm asking if the file already exist and if it does, it adds number to the file name and it does in such a way that it keeps the file format.我已经取了文件的名称,我在问该文件是否已经存在,如果存在,它会在文件名中添加数字,并以保留文件格式的方式进行。 But if you don't want to save it with the file format you can do it without the fileName.Split();但是如果你不想用文件格式保存它,你可以不用fileName.Split(); just by adding the number behind the file name newFileName = fileName+$"_{i}";只需在文件名后面添加数字newFileName = fileName+$"_{i}"; . . If this helped mark this as the answer.如果这有助于将其标记为答案。

//if you want you can use this method or just copy the code you need
void Unique(Image input)
{
    string fileName = "filename.jpg";
    string newFilename = null;
    for(int i = 1; true; i++)

        //this is so that you can alter the name and keep the file format 
        newFileName = filename.Split('.')[0]+"_{i}"+filename.Split('.')[1];

        if(!File.Exist(newFileName))
        {   
            break;
        }
    }
    return Image.ToBitmap().Save(newFileName,ImageFormat.Png);
} 

This answer is made for WinForms, but same principles should apply to to all the UI frameworks.这个答案是为 WinForms 做出的,但同样的原则应该适用于所有的 UI 框架。

If you want to be able to choose the folder in run time, then you can use this code:如果您希望能够在运行时选择文件夹,那么您可以使用以下代码:

 void Unique(Image input)
    {
        string fileName = "filename.jpg";
        string newFileName = null;

        //Crates the dialog window
        var dirDialog = new FolderBrowserDialog();
        if (dirDialog.ShowDialog() == DialogResult.OK)
        {
            newFileName = dirDialog.SelectedPath + fileName;

            for (int i = 1; true; i++)
            {


                //this is so that you can alter the name and keep the file format 
                newFileName = fileName.Split('.')[0] + "_{i}" + fileName.Split('.')[1];

                if (!File.Exists(newFileName))
                {
                    break;
                }

            }
            //save the file
            new Bitmap(input).Save(newFileName, ImageFormat.Png);
        }

        //deletes the dialog window from memory
        dirDialog.Dispose();
    }

But keep in mind that this code will ask you for the folder every time that you are going to save the file.请记住,每次您要保存文件时,此代码都会要求您提供文件夹。 So if you are going to save multiple files at once, I would advise you to save the dirDialog.SelectedPath in some string variable.因此,如果您要一次保存多个文件,我建议您将dirDialog.SelectedPath保存在某个string变量中。

If this helped mark this as the answer.如果这有助于将其标记为答案。

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

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