[英]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.