我已經做了大量的研究,並在這裏看了很多問題,但似乎無法找到任何幫助我的東西。我應該序言我對C#,Windows Forms和SO非常陌生!我是來自C++的第一年CompSci學生,在夏季嘗試我自己的項目。我正嘗試使用AForge.Video.FFMPEG視頻文件閱讀器顯示來自.avi的一系列位圖。來自AForge FFMPEG空的圖片框空 - C#/ WinForms
這似乎是找到該文件,得到它的數據(控制檯打印尺寸,幀率和編解碼器)和創建PictureBox的,但PictureBox的出現空白/空。我從一個.AVI的幀得到位圖:
然後,我試圖用一個圖片框來顯示它:
From MS example code here as well
這裏是我的代碼。本質上是兩者的組合:
public class Simple : Form
{
Bitmap videoFrame;
public Simple()
{
try
{
// create instance of video reader
VideoFileReader reader = new VideoFileReader();
// open video file
reader.Open(@"C:\Users\User\Desktop\ScanTest3.AVI");
// check some of its attributes
Console.WriteLine("width: " + reader.Width);
Console.WriteLine("height: " + reader.Height);
Console.WriteLine("fps: " + reader.FrameRate);
Console.WriteLine("codec: " + reader.CodecName);
PictureBox pictureBox1 = new PictureBox();
// read 100 video frames out of it
for (int i = 0; i < 100; i++)
{
videoFrame = reader.ReadVideoFrame();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.ClientSize = new Size(videoFrame.Width, videoFrame.Height);
pictureBox1.Image = videoFrame;
// dispose the frame when it is no longer required
videoFrame.Dispose();
}
reader.Close();
}
catch
{
Console.WriteLine("Nope");
}
}
}
class MApplication
{
public static void Main()
{
Application.Run(new Simple());
}
}
所以這就是它。只是一個空白的圖片框,當它應該有視頻的第一幀時,即使沒有發現異常(儘管我非常有信心我使用try/catch的效果很差),並且控制檯打印正確的數據該文件:
width: 720
height: 480
fps: 29
codec: dvvideo
[swscaler @ 05E10060] Warning: data is not aligned! This can lead to a speedloss
雖然如果有人能告訴我這是什麼警告的手段,那將是偉大的,以及,但我主要只是失去了,爲什麼沒有圖片打印到屏幕上。
謝謝!
如何刪除'videoFrame.Dispose();' –
@LeiYang如果'videoFrame.Dispose();'被刪除,問題仍然存在。我相信VideoFileReader需要在下一個循環中移動到下一幀 –
如果不用於循環,只顯示其中一個幀,該怎麼辦? –