我正在使用Magick.NET和C#Console。下面的代码找到所有.eps
文件,然后将其转换为.jpg
。
foreach (string fileName in Directory.GetFiles("C:/Users/Adrian/Documents/Visual Studio 2010/Projects/ImageMagickTest/ImageMagickTest/bin/Debug/eps","*.eps"))
{
using (MagickImage image = new MagickImage())
{
Console.WriteLine("\n\nNow Converting. Please Wait...\n\n");
image.Read(fileName, settings);
image.Write(fileName.Substring(0,fileName.Length - 3) + ".jpg");
i++;
Console.WriteLine("Conversion Success.\n\n");
Console.WriteLine("Files Converted: " + i);
}
}
现在,我想要的是将.eps与.jpg分开。 例如:
文件夹/eps/
内容:
执行循环后。文件夹/eps/
的内容为:
如何将.jpg放在不同的文件夹中?
答案 0 :(得分:0)
我假设你想要一个jpg文件夹?添加此项以在foreach循环之前创建文件夹。
if (!Directory.Exists("C:/Users/Adrian/Documents/Visual Studio 2010/Projects/ImageMagickTest/ImageMagickTest/bin/Debug/jpg"))
{
Directory.CreateDirectory("C:/Users/Adrian/Documents/Visual Studio 2010/Projects/ImageMagickTest/ImageMagickTest/bin/Debug/jpg");
}
我还假设image.Write
实际上会创建文件,如果是,那么你可以更改以下行
image.Write(fileName.Substring(0,fileName.Length - 3) + ".jpg");
是这样的
image.Write(fileName.Substring(0,fileName.Length - 7) + "jpg/.jpg");
答案 1 :(得分:0)
E.g。
// Set the source folder to whatever.
var sourceFolderPath = "...";
// Create the destination folder path by removing the leaf folder name and replacing it with another.
var destinationFolderPath = Path.Combine(Path.GetDirectoryName(sourceFolderPath), "jpg");
foreach (var sourceFilePath in Directory.GetFiles(sourceFolderPath, "*.eps"))
{
// Build the destination file path from the desitination folder path, file name and a new extension.
var destinationFilePath = Path.ChangeExtension(Path.Combine(destinationFolderPath, Path.GetFileName(sourceFilePath)), ".jpg");
// Use destinationFilePath here.
}
答案 2 :(得分:0)
我认为此代码将为您提供您想要的内容
foreach (string fileName in Directory.GetFiles("Folder","*.eps"))
{
using (MagickImage image = new MagickImage())
{
Console.WriteLine("\n\nNow Converting. Please Wait...\n\n");
image.Read(fileName, settings);
string[] split = filename.Split('\\');
string clear_file_name = split[split.Length-1];
string split_file_name= clear_file_name.split('.');
string filename_without_extention = split_file_name[0];
if(!Directory.Exists(folder+"\\jpeg"))
Directory.Create(folder+"\\jpeg");
image.Write(fileName.Substring(0,folder+"\\jpeg\\"+file_name_without_extention+".jpg");
i++;
Console.WriteLine("Conversion Success.\n\n");
Console.WriteLine("Files Converted: " + i);
}
}