how to create a thumbnail from video in window form c#

Sep 21 2016 6:12 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Sample_Thumbnail
{
public partial class Form : System.Windows.Forms.Form
{
private Image imgThumb;
public Form()
{
InitializeComponent();
}
public static string generateThumb(string file)
{
string thumb = "";
try
{
FileInfo fi = new FileInfo(file);
string filename = Path.GetFileNameWithoutExtension(fi.Name);
Random random = new Random();
int rand = random.Next(1, 9999999);
string newfilename = "C:\\Users\\user\\Desktop\\Preethi\\Sample_Thumbnail\\Sample_Thumbnail\\bin\\Debug\\" + filename + "___(" + rand.ToString() + ").jpg";
var processInfo = new ProcessStartInfo();
processInfo.FileName = @"C:\Users\user\Desktop\Preethi\Sample_Thumbnail\Sample_Thumbnail\bin\Debug\ffmpeg.exe";
processInfo.Arguments = string.Format("-ss {0} -i {1} -f image2 -vframes 1 -y {2}", 5, "\"" + file + "\"", "\"" + newfilename + "\"");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
using (var process = new Process())
{
process.StartInfo = processInfo;
process.Start();
process.WaitForExit();
thumb = newfilename;
}
}
catch (Exception ex)
{
string error = ex.Message;
}
return thumb;
}
private void Generate_Click(object sender, EventArgs e)
{
string thumb = "";
Image image = null;
// Check if textbox has a value
if (txtFileNm.Text != String.Empty)
thumb = generateThumb(txtFileNm.Text);
image = Image.FromFile(thumb.ToString());
if (image != null)
{
imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());
// Refresh();
}
pictureBox1.Image = imgThumb;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (imgThumb != null)
e.Graphics.DrawImage(imgThumb, 30, 20, imgThumb.Width, imgThumb.Height);
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtFileNm.Text = openFileDialog1.FileName;
}
}
}
}

Answers (3)