使用 Emgu CV 将图像保存到文件夹
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
变量中。
我有一些图像是使用 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
变量中。