效果
项目
代码
using nlog; using opencvsharp; using system; using system.collections.generic; using system.io; using system.linq; using system.windows.forms; namespace opencvsharp_demo { public partial class form1 : form { public form1() { initializecomponent(); nlog.windows.forms.richtextboxtarget.reinitializealltextboxes(this); } private static logger _log = nlog.logmanager.getcurrentclasslogger(); private void form1_load(object sender, eventargs e) { } string inpath = ""; string outpath = ""; directoryinfo folder; list<fileinfo> files=new list<fileinfo>(); string[] imageextensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }; /// <summary> /// 选择文件夹 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_click(object sender, eventargs e) { inpath = ""; outpath = ""; files.clear(); folderbrowserdialog dialog = new folderbrowserdialog(); dialog.description = "请选择文件路径"; if (dialog.showdialog() == dialogresult.ok) { inpath = dialog.selectedpath; textbox1.text = inpath; outpath = inpath + "\\out"; textbox2.text = outpath; _log.info("图片路径:" + inpath); _log.info("保存路径:" + outpath); folder = new directoryinfo(inpath); var temp = folder.getfiles("*.*", searchoption.topdirectoryonly); foreach (fileinfo file in temp) { if (imageextensions.contains(file.extension.tolower())) { files.add(file); } } _log.info("一共["+ files .count()+ "]张图片"); } } /// <summary> /// 修改名称 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_click(object sender, eventargs e) { if (files.count()==0) { return; } outpath = textbox2.text; //目录不存在 则创建 if (!directory.exists(outpath)) { directoryinfo directoryinfo = new directoryinfo(outpath); //创建目录 directoryinfo.create(); } else { directoryinfo outfolder=new directoryinfo(outpath); if (outfolder.getfiles("*.*", searchoption.alldirectories).length>0) { messagebox.show(outpath + "文件夹不为空,防止数据被覆盖,请更换!"); return; } } string oldname; string newname; mat temp; int index = 0; foreach (fileinfo file in files) { oldname = file.name; newname = index.tostring() + file.extension; try { temp = new mat(inpath + "\\" + oldname); //其他处理 ,例如 //图片缩放 //通道变换等 //…… cv2.imwrite(outpath + "\\" + newname, temp); _log.info(oldname + "-->" + newname); index++; } catch (exception ex) { _log.info(oldname+"修改异常,异常信息:"+ex.message); } } _log.info("全部修改完成!"); } } }
到此这篇关于c# opencvsharp实现图片批量改名的文章就介绍到这了,更多相关c# opencvsharp图片改名内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论