[英]Save images to folder using Emgu CV
我有一些使用Emgu CV从其他图像中提取的图像。
我试图将这些图像保存到一个文件夹中,但只有最后一张图像与我的代码一起保存到文件夹中。
Image.ToBitmap().save("filename",imageformat.png)
如何将所有图像保存在一个文件夹中?
你可以尝试做这样的事情。 我已经取了文件的名称,我在问该文件是否已经存在,如果存在,它会在文件名中添加数字,并以保留文件格式的方式进行。 但是如果你不想用文件格式保存它,你可以不用fileName.Split();
只需在文件名后面添加数字newFileName = fileName+$"_{i}";
. 如果这有助于将其标记为答案。
//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);
}
这个答案是为 WinForms 做出的,但同样的原则应该适用于所有的 UI 框架。
如果您希望能够在运行时选择文件夹,那么您可以使用以下代码:
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();
}
但请记住,每次您要保存文件时,此代码都会要求您提供文件夹。 因此,如果您要一次保存多个文件,我建议您将dirDialog.SelectedPath
保存在某个string
变量中。
如果这有助于将其标记为答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.