C#: How to programmatically resize images or create thumbnails from a variety of differently sized images using ffmpeg

ImageMagick should be used to do this,  ffmpeg is for AV.

But to do this using ffmpeg and c#, follow these steps….

You can download ffmpeg for free at http://ffmpeg.org/download.html

My images exists in C:\image_directory and my ffmpeg exists in C:\ffmpeg.

 

I will be using the following ffmpeg command to resize each image (create a thumbnail):

C:\ffmpeg\bin>ffmpeg -i c:\image_directory\1.png -s 80×80 c:\image_directory\t\t_1.png


The above command will take an input image 1.png, resize it to 80px by 80px, save it in a sub-folder named \t\,
and append “t_” to the begining of the file name.

Here is how you would loop through every image in the folder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
 
namespace ImgResize
{
    class Program
    {
        static void Main(string[] args)
        {
            //get an array of all the image files//
            string[] imgPaths = Directory.GetFiles(@"c:\image_directory\");
 
            //loop through each image//
            for (int i = 0; i < imgPaths.Length; i++)
            {
                string file_path = imgPaths[i];
                //get the file name out of full path//
                string filename = Regex.Match(imgPaths[i], @".*\\([^\\]+$)").Groups[1].Value;
                //output the filename about to be converted
                Console.WriteLine("converting : "+filename);
                //make arguements string
                string write2string = " -i " + file_path + " -s 80x80 "+@"c:\\image_directory\\t\\t_" + filename;
                //create a process
                Process myProcess = new Process();
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.RedirectStandardOutput = true;
                //point ffmpeg location
                myProcess.StartInfo.FileName = @"c:\\ffmpeg\\bin\\ffmpeg";
                //set arguements
                myProcess.StartInfo.Arguments = write2string;
                myProcess.Start();
                myProcess.WaitForExit();
            }
        }
    }
}

3 thoughts on “C#: How to programmatically resize images or create thumbnails from a variety of differently sized images using ffmpeg

  1. AKE

    While this is correct, it seems a bit excessive when you could simply do:

    for %%f in (.\*.jpg) do ffmpeg.exe -i %%f -vf scale=-1:80 .\t\t_%%f

    which would loop through all jpg’s in a given folder (here the current folder) and create thumbnails while preserving the aspect ratio (recommended!).

    No need for C# (sledgehammer, mosquito, etc.)

    Reply
  2. Jonathan Annett

    Ok am I missing something here?

    Ffmpeg lets you specify a naming convention for images on input and output

    If you have img1.png thru img100,png use -i img%d.png

    It stops when it runs out of frames

    there is no need for any scripting at all

    You can even make a slide show of your thumbnails as an mov file and add a soundtrack if that’s your thang

    Reply

Leave a Reply to Deep Cancel reply

Your email address will not be published.