mo.notono.us

Friday, April 13, 2007

Simple Batch Jpg to Png Script in C#

Compile the following using SnippetCompiler
using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace JpgToPng
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Path of the Folder containing the Image Files to Convert :");
      string inputFolder = Console.ReadLine();
      Console.WriteLine("Path of the Output Folder:");
      string outputFolder = Console.ReadLine();
      int count = BatchConvertJpgToPng(inputFolder, outputFolder);
      Console.WriteLine(new string('-', 60));
      Console.WriteLine("Converted {0} files - see {1}", count, outputFolder);
      Console.ReadLine();
    }
    public static int BatchConvertJpgToPng(string inputFolder, string outputFolder)
    {
      //basic error handling
      if (!Directory.Exists(outputFolder))
        Directory.CreateDirectory(outputFolder);
      
      int i = 0;
      DirectoryInfo inDI = new DirectoryInfo(inputFolder);
      string outputPath;
      foreach (FileInfo inFI in inDI.GetFiles("*.jpg"))
      {
        outputPath = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(inFI.FullName)) + ".png";
        new Bitmap(inFI.FullName).Save(outputPath, ImageFormat.Png);
        Console.WriteLine("Created {0}", outputPath);
        i++;
      }
      return i;
    }
  }
}

0 Comments:

Post a Comment

<< Home