我正在使用 Magick.NET 和C#控制台。下面的代码找到所有.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("nnNow Converting. Please Wait...nn");
image.Read(fileName, settings);
image.Write(fileName.Substring(0,fileName.Length - 3) + ".jpg");
i++;
Console.WriteLine("Conversion Success.nn");
Console.WriteLine("Files Converted: " + i);
}
}
现在,我想要的是将.eps与.jpg分开。例如:
文件夹/eps/
内容:
- 图片1.eps
- 图像2.eps
执行循环后。 文件夹/eps/
内容将是:
- 图片1.eps
- 图像2.eps
- 图片1.jpg
- 图像2.jpg
如何将.jpg放在其他文件夹中?
我假设你想要一个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");
我认为这段代码会给你你想要的
foreach (string fileName in Directory.GetFiles("Folder","*.eps"))
{
using (MagickImage image = new MagickImage())
{
Console.WriteLine("nnNow Converting. Please Wait...nn");
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.nn");
Console.WriteLine("Files Converted: " + i);
}
}
例如
// 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.
}