Skip to main content

WPF C#/VB
Making a Slideshow Video from a Set of Images



C#

// AZUL CODING ---------------------------------------
// WPF C#/VB - Making a Slideshow Video from a Set of Images
// https://youtu.be/_MKB1er5IqE


using Accord.Video.FFMPEG;

namespace Video
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        readonly System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog()
        {
            Title = "Choose images",
            InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Azul Coding",
            Filter = "Image files|*.png;*.jpg",
            Multiselect = true
        };

        private void VideoBtn_Click(object sender, RoutedEventArgs e)
        {
            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (var vFWriter = new VideoFileWriter())
                {
                    vFWriter.Open(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos) + "\\video.mp4", 1920, 1080, 10, VideoCodec.H264);

                    foreach (var imgpath in openDialog.FileNames)
                    {
                        var bmp = new System.Drawing.Bitmap(1920, 1080);

                        using (var g = System.Drawing.Graphics.FromImage(bmp))
                        {
                            g.Clear(System.Drawing.Color.White);

                            System.Drawing.Bitmap img = new System.Drawing.Bitmap(imgpath);
                            System.Drawing.Point p;

                            var ratio = 1080 / (double)img.Height;
                            img = new System.Drawing.Bitmap(img, (int)Math.Round(img.Width * ratio, 0), 1080);

                            p = new System.Drawing.Point((int)Math.Round((1920 - img.Width) / 2.0, 0), (int)Math.Round((1080 - img.Height) / 2.0, 0));

                            g.DrawImage(img, p);

                            for (var i = 1; i <= 20; i++)
                                vFWriter.WriteVideoFrame(bmp);
                        }
                    }
                    vFWriter.Close();
                }
                MessageBox.Show("Video saved.");
            }
        }
    }
}

Enjoying this tutorial?


VB.NET

' AZUL CODING ---------------------------------------
' WPF C#/VB - Making a Slideshow Video from a Set of Images
' https://youtu.be/_MKB1er5IqE


Imports Accord.Video.FFMPEG

Class MainWindow

    ReadOnly openDialog As System.Windows.Forms.OpenFileDialog = New System.Windows.Forms.OpenFileDialog() With {
        .Title = "Choose images",
        .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) & "\Azul Coding",
        .Filter = "Image files|*.png;*.jpg",
        .Multiselect = True
    }

    Private Sub VideoBtn_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If openDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

            Using vFWriter = New VideoFileWriter()
                vFWriter.Open(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos) & "\video.mp4", 1920, 1080, 10, VideoCodec.H264)

                For Each imgpath In openDialog.FileNames
                    Dim bmp = New System.Drawing.Bitmap(1920, 1080)

                    Using g = System.Drawing.Graphics.FromImage(bmp)
                        g.Clear(System.Drawing.Color.White)

                        Dim img As System.Drawing.Bitmap = New System.Drawing.Bitmap(imgpath)
                        Dim p As System.Drawing.Point

                        Dim ratio = 1080 / img.Height
                        img = New System.Drawing.Bitmap(img, Math.Round(img.Width * ratio, 0), 1080)

                        p = New System.Drawing.Point(Math.Round((1920 - img.Width) / 2.0, 0), Math.Round((1080 - img.Height) / 2.0, 0))
                        g.DrawImage(img, p)

                        For i = 1 To 20
                            vFWriter.WriteVideoFrame(bmp)
                        Next
                    End Using
                Next

                vFWriter.Close()
            End Using

            MessageBox.Show("Video saved.")
        End If
    End Sub
End Class

XAML

<!-- AZUL CODING --------------------------------------- -->
<!-- WPF C#/VB - Making a Slideshow Video from a Set of Images -->
<!-- https://youtu.be/_MKB1er5IqE -->


<Window x:Class="Video.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Video"
        mc:Ignorable="d"
        Title="Video" Height="136" Width="261" WindowStartupLocation="CenterScreen">
    <Grid>
        <Button x:Name="VideoBtn" Content="Select files" HorizontalAlignment="Left" Height="46.753" Margin="44.968,27.169,0,0" VerticalAlignment="Top" 
                Width="163.636" Click="VideoBtn_Click" FontSize="16"/>
    </Grid>
</Window>