0

我正在使用 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("\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/内容:

  • image1.eps
  • image2.eps

执行循环后。文件夹/eps/内容将是:

  • image1.eps
  • image2.eps
  • 图像1.jpg
  • 图像2.jpg

我如何将 .jpg 放在不同的文件夹中?

4

3 回答 3

0

例如

// 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.
}
于 2014-02-12T09:03:28.097 回答
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); 
                }
            }
于 2014-02-12T09:06:43.157 回答
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");
于 2014-02-12T08:59:48.583 回答