HOW TO: Generate Thumbnails on the Fly using ASP.NET while uploading video file

.NET, C# Add comments

In this tutorial our focus is on creating dynamic thumbnails with the help of .NET framework. Here is the scenario: We have simple aspx page with FileUpload and Button control. We will use FMPEG library, so you can have it installed before you start this example.
Here is the code of aspx page:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>








and code for web.config file:




  
    
    
  

	
	
		
		
    
	

as you can see, we save location of fmpeg and thumb path in web.config.

and here is the code which will uplaod video and save it’s thumb:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string thumbpath, thumbname, inputfile;
        string thumbargs;        

        thumbpath = ConfigurationManager.AppSettings.GetValues("thumbpath")[0];

        inputfile = thumbpath + fileUpload.FileName; 

        fileUpload.PostedFile.SaveAs( inputfile);

        thumbname = thumbpath + fileUpload.PostedFile.FileName.Substring(0, fileUpload.PostedFile.FileName.LastIndexOf(".")) + ".jpg";
        thumbargs = "-i " + inputfile + " -f image2 -ss 1 -vframes 1 -s 150x150 " + thumbname; 

        Process thumbproc = new Process();
        thumbproc = new Process();
        thumbproc.StartInfo.FileName = thumbpath = ConfigurationManager.AppSettings.GetValues("ffmpegPath")[0];
        thumbproc.StartInfo.Arguments = thumbargs;
        thumbproc.StartInfo.UseShellExecute = false;
        thumbproc.StartInfo.CreateNoWindow = false;
        thumbproc.StartInfo.RedirectStandardOutput = false;
        try
        {
            thumbproc.Start();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        thumbproc.WaitForExit();
        thumbproc.Close();
    }
}

Leave a Reply

You must be logged in to post a comment.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in