c#中的单图像到视频

本文关键字:图像 视频 单图像 | 更新日期: 2023-09-27 18:08:15

我一直在VS中使用Aforge来操纵图像,现在我需要将单个图像转换为视频。我有作品的代码,但只要我添加一个图像路径它输出一个空视频。我想这是非常简单的事情,但由于我对c#几乎一无所知,所以我需要帮助的过程中的每一步。

有人能帮我一下吗?
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();
        }
    }
顺便说一下,任何其他不包括库或框架的解决方案都是受欢迎的。我只是使用Aforge,因为我相信这是最简单的方法。

Thanks in advance

c#中的单图像到视频

我可以用以下代码从JPEG图像创建一个视频:

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();

你需要调整图像的大小以匹配视频帧的大小。