2016-09-21 26 views
1

我一直在VS中使用Aforge來操縱圖像,現在我需要將單個圖像轉換爲視頻。代碼我有工作,但只要我添加一個圖像路徑它輸出一個空的視頻。我猜想是非常簡單的事情,但是因爲我實際上對c#一無所知,我需要幫助每一步的幫助。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,因爲它是最簡單的方法。

在此先感謝

回答

1

我能夠創建一個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(); 

你需要調整圖像匹配到視頻的大小幀。

+0

上帝保佑你狡猾! 這將是我的第三次嘗試與答案。所以我今天將會試用這個代碼,我會回到這裏讓你和每個人都知道發生了什麼。 再次感謝! – fauvent

+0

非常棒!非常感謝!!!! – fauvent

+0

不客氣,很高興我能幫助 – sly