Last active
August 29, 2015 14:23
-
-
Save aloisdg/a86cec1d4db39931d0cf to your computer and use it in GitHub Desktop.
A simple script to convert quickly a bunch of png to jpg.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Nito.AsyncEx; | |
namespace PngToJpg | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
AsyncContext.Run(() => MainAsync(args)); | |
} | |
private static async void MainAsync(IEnumerable<string> args) | |
{ | |
var tasks = new Collection<Task>(); | |
foreach (var arg in args.Where(a => Path.GetExtension(a) != null | |
&& Path.GetExtension(a).Equals(".png"))) | |
{ | |
tasks.Add(ConvertToJpg(arg)); | |
} | |
await Task.WhenAll(tasks); | |
} | |
private static Task ConvertToJpg(string path) | |
{ | |
return Task.Run(() => | |
{ | |
var image = Image.FromFile(path); | |
var newPath = Path.ChangeExtension(path, ".jpg"); | |
using (var b = new Bitmap(image.Width, image.Height)) | |
{ | |
b.SetResolution(image.HorizontalResolution, image.VerticalResolution); | |
using (var g = Graphics.FromImage(b)) | |
{ | |
g.Clear(Color.White); | |
g.DrawImageUnscaled(image, 0, 0); | |
} | |
b.Save(newPath, ImageFormat.Jpeg); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment