我最近想尝试一下锻造网 http://www.aforgenet.com/framework/因为我发现它非常简单,所以我决定使用 Video.FFMPEG 命名空间进行一些简单的视频播放,这样我就可以将每个帧直接放在 pictureBox 上。仅此一项效果就很好,但我想在不重要之后处理每张图像,因为它无缘无故地占用了大约 1.5GB 内存。这就是我的问题开始的地方。由于某种原因,它有时会抛出此异常(通常在调整窗口大小时)。我不太确定这可能是由什么引起的。也许这真的是一个愚蠢的错误。我的猜测是,这可能是由计时器引起的,但我可能犯了一个完全不同的错误,只是看不到它。这是我不断收到的异常:
************** Exception Text **************
System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
这是代码(我确实意识到公共变量不好,我当然只是测试):
public long i = 0;
public Bitmap img;
public VideoFileReader reader;
public System.Timers.Timer aTimer;
public void render(object source, ElapsedEventArgs e)
{
if (img != null) img.Dispose();
if (i < reader.FrameCount)
{
img = reader.ReadVideoFrame();
pictureBox1.Image = img;
}
i++;
}
private void button1_Click(object sender, EventArgs e)
{
reader = new VideoFileReader();
aTimer = new System.Timers.Timer();
reader.Open("d:\\result.avi");
aTimer.Elapsed += new ElapsedEventHandler(render);
aTimer.Interval = reader.FrameRate;
aTimer.Enabled = true;
}
我想我在计时器方面错过了一些东西,它们似乎不太适合这种情况。对于想要使用 AForge.NET 进行播放的人来说,这可能是一个解决方案。我推迟了计时器并使用带有秒表的backgroundWorker,到目前为止没有出现任何问题。
public Image img;
public VideoFileReader reader;
private void button1_Click(object sender, EventArgs e)
{
reader = new VideoFileReader();
reader.Open("d:\\result.avi");
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Stopwatch watch = new Stopwatch();
for (i=0;i<reader.FrameCount;i++)
{
img = pictureBox1.Image;
pictureBox1.Image = reader.ReadVideoFrame();
if (img != null) img.Dispose();
watch.Start();
while (watch.ElapsedMilliseconds < reader.FrameRate);
watch.Stop();
watch.Reset();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)