【发布时间】:2018-01-06 13:57:34
【问题描述】:
我已经进行了大量研究,并在这里查看了很多问题,但似乎找不到任何可以帮助我的东西。我应该先介绍一下我对 C#、Windows 窗体和 SO 非常陌生!我是来自 C++ 的一年级 CompSci 学生,我在暑假尝试自己的项目。我正在尝试使用 AForge.Video.FFMPEG 视频文件阅读器显示来自 .avi 的一系列位图。
似乎正在查找文件,获取其数据(控制台打印尺寸、帧速率和编解码器)并创建图片框,但图片框出现空白/空强>。我从 .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 有必要在下一个循环中移动到下一帧 -
如果不使用 for 循环并且只显示其中一个帧怎么办?
-
@LeiYang 尝试了循环外的图片框和 videofilereader 声明,问题仍然存在
-
为什么要在运行时创建
PictureBox?
标签: c# winforms ffmpeg picturebox aforge