Single image to video in c#

拜拜、爱过 提交于 2021-01-24 11:46:44

问题


I've been using Aforge in VS to manipulate images, and now I need to convert a single image to video. The code I have works, but as soon as I add an image path it outputs an empty video. I guess is something very simple, but since i know practicly nothing about c#, I need help every step of the process.

Can someone please help me with this?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Video.FFMPEG;


namespace VideoWriter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
        InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            int width = 720;
            int height = 402;

            VideoFileWriter writer = new VideoFileWriter();
            writer.Open(@"C:\pathtovideo\video.mp4", width, height, 25, VideoCodec.MPEG4, 1000000);

            Bitmap image = new Bitmap(width, height);

            // THIS ONE NEXT IS WHAT I HAVE BEEN TRYING, BUT I GUESS IS VERY WRONG
            //Bitmap image = new Bitmap(@"C:\pathtoimage\myimage.jpg");

            for (int i = 0; i < 250; i++)
            {
                writer.WriteVideoFrame(image);
            }

            writer.Close();

        }
    }

By the way, any other solution not including libraries or frameworks is wellcome. I am just using Aforge because is the easiest way, I believe.

Thanks in advance


回答1:


I'm able to create a video from a JPEG image with the following code:

int width = 720;
int height = 402;

VideoFileWriter writer = new VideoFileWriter();
writer.Open(@"C:\Temp\video.mp4", width, height, 25, VideoCodec.MPEG4, 1000000);

Bitmap originalImage = new Bitmap(@"C:\Temp\myimage.jpg");
Bitmap resizedImage = new Bitmap(originalImage, new Size(width, height));

for (int i = 0; i < 250; i++)
{
    writer.WriteVideoFrame(resizedImage);
}

writer.Close();

You'll need to resize the image to match to size of the video frame.



来源:https://stackoverflow.com/questions/39625554/single-image-to-video-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!