2014-02-12 91 views
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); 
        } 
       } 

現在,我想要的是從.jpg中分離.eps。 例如:

/eps/夾內容:

  • image1.eps
  • image2.eps

執行循環之後。該文件夾的內容/eps/將是:

  • image1.eps
  • image2.eps
  • image1.jpg
  • image2.jpg

我將如何放置.JPG不同夾?

+0

我也許檢查你的答案明天或週五到底是什麼。 GTG。 –

回答

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"); 
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. 
} 
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); 
       } 
      } 
相關問題